NoSQL数据库Cassandra监控

使用packetbeat、elasticsearch、logstash、kibana为cassandra搭建监控系统。

创建用户

1
2
3
groupadd add es
useradd -g es es
passwd es

ES安装

下载

1
2
3
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.0.0.zip
unzip elasticsearch-6.0.0.zip
./bin/elasticsearch-plugin install x-pack

修改配置vim config/elasticsearch.yml(未使用集群)

1
network.host: 0.0.0.0

设置内置用户密码(需要启动)

1
2
3
4
bin/x-pack/setup-passwords interactive
elastic:elastic
kibana:kibana
logstash_system:logstash_system

生成随机密码,建议生产环境使用

1
bin/x-pack/setup-passwords auto

启动:

1
./bin/elasticsearch -d

验证
浏览器中http://10.112.68.192:9200/,输入用户名密码(elastic:elastic)登录,检查es安装是否成功。

kibana安装

下载

1
2
3
wget https://artifacts.elastic.co/downloads/kibana/kibana-6.0.0-linux-x86_64.tar.gz
tar zxvf kibana-6.0.0-linux-x86_64.tar.gz
bin/kibana-plugin install x-pack

配置vim config/kibana.yml

1
2
3
4
5
6
7
server.port: 5601
server.host: "10.112.68.192"
server.name: "es-kibana"
elasticsearch.url: "http://localhost:9200"
ibana.index: ".kibana"
elasticsearch.username: "elastic"
elasticsearch.password: "elastic"

启动

1
nohup ./bin/kibana &

浏览器中输入http://10.112.68.192:5601/ 输入用户名密码登录(kibana:kibana,kibana权限不够请使用elastic用户设置),检查kibana安装是否成功。

Packetbeat安装

Packetbeat 是 Elastic 开源的网络流量实时监控工具,目前支持了一些流行的应用软件,如MongoDB、Redis、MySQL、Cassandra等。

下载

1
2
wget https://artifacts.elastic.co/downloads/beats/packetbeat/packetbeat-6.0.0-linux-x86_64.tar.gz
sudo yum install libpcap

配置vim packetbeat.yml(其他数据收集关掉,只开启cassandra)

1
2
3
4
5
6
7
8
9
10
11
12
13
packetbeat.interfaces.device: any
- type: cassandra
ports: [9042]
setup.dashboards.enabled: true
setup.kibana:
host: "10.112.68.192:5601"
username: "kibana"
password: "kibana"
output.elasticsearch:
# Array of hosts to connect to.
hosts: ["10.112.68.192:9200"]
username: "elastic"
password: "elastic"

启动

1
nohup sudo ./packetbeat -e -v &

验证

编写JAVA测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.demo.simple;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

/**
* Created by luoji on 04/12/2017.
*/
public class CreateAndPopulateKeyspace {

static String[] CONTACT_POINTS = {"10.112.68.186","10.112.68.192"};
static int PORT = 9042;

public static void main(String[] args) {

CreateAndPopulateKeyspace client = new CreateAndPopulateKeyspace();

try {

client.connect(CONTACT_POINTS, PORT);
client.createSchema();
client.loadData();
client.querySchema();

} finally {
client.close();
}
}

private Cluster cluster;

private Session session;

/**
* Initiates a connection to the cluster
* specified by the given contact point.
*
* @param contactPoints the contact points to use.
* @param port the port to use.
*/
public void connect(String[] contactPoints, int port) {

cluster = Cluster.builder()
.addContactPoints(contactPoints).withPort(port)
.build();

System.out.printf("Connected to cluster: %s%n", cluster.getMetadata().getClusterName());

session = cluster.connect();
}

/**
* Creates the schema (keyspace) and tables
* for this example.
*/
public void createSchema() {

session.execute("CREATE KEYSPACE IF NOT EXISTS simplex WITH replication " +
"= {'class':'SimpleStrategy', 'replication_factor':1};");

session.execute(
"CREATE TABLE IF NOT EXISTS simplex.songs (" +
"id uuid PRIMARY KEY," +
"title text," +
"album text," +
"artist text," +
"tags set<text>," +
"data blob" +
");");

session.execute(
"CREATE TABLE IF NOT EXISTS simplex.playlists (" +
"id uuid," +
"title text," +
"album text, " +
"artist text," +
"song_id uuid," +
"PRIMARY KEY (id, title, album, artist)" +
");");
}

/**
* Inserts data into the tables.
*/
public void loadData() {

session.execute(
"INSERT INTO simplex.songs (id, title, album, artist, tags) " +
"VALUES (" +
"756716f7-2e54-4715-9f00-91dcbea6cf50," +
"'La Petite Tonkinoise'," +
"'Bye Bye Blackbird'," +
"'Joséphine Baker'," +
"{'jazz', '2013'})" +
";");

session.execute(
"INSERT INTO simplex.playlists (id, song_id, title, album, artist) " +
"VALUES (" +
"2cc9ccb7-6221-4ccb-8387-f22b6a1b354d," +
"756716f7-2e54-4715-9f00-91dcbea6cf50," +
"'La Petite Tonkinoise'," +
"'Bye Bye Blackbird'," +
"'Joséphine Baker'" +
");");
}

/**
* Queries and displays data.
*/
public void querySchema() {

ResultSet results = session.execute(
"SELECT * FROM simplex.playlists " +
"WHERE id = 2cc9ccb7-6221-4ccb-8387-f22b6a1b354d;");

System.out.printf("%-30s\t%-20s\t%-20s%n", "title", "album", "artist");
System.out.println("-------------------------------+-----------------------+--------------------");

for (Row row : results) {

System.out.printf("%-30s\t%-20s\t%-20s%n",
row.getString("title"),
row.getString("album"),
row.getString("artist"));

}

}

/**
* Closes the session and the cluster.
*/
public void close() {
if (session == null) {
return;
}
session.close();
cluster.close();
}

}

浏览器中查看,可以看到刚才对cassandra所做的操作。
Alt text