forked from guoxuce/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path监控--1
454 lines (362 loc) · 18 KB
/
监控--1
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
开源软件(这三种都是php写出来的网站程序)
cacti 重图形,有数据历史,需用到数据库支持,支持web配置,默认不支持告警,可以加插件;
nagios重状态和结果,没有数据历史,不成图像,不支持web配置,可以自己开发脚本定制个性化监控,支持多种插件;
zabbix有数据历史,可成图像,支持web配置,可以自动发现;
cacti(rrdtool)监控网络设备,绘图(记录数据),数据存储(mysql)
nagios 服务器状态(不记录历史) 一目了然看出系统服务是否正常,只返回数据,不记录
可以自己写脚本,报警功能(邮件、短信)
只返回状态,可以设置阈值,
客户端,服务端
zabbix 比nagios更丰富 ,支持历史,支持画图(数据存储mysql),报警(邮件、短信),自动将新服务器加入服务列表
(社区内有监控模板)
第一部分:cacti安装配置
1. 首先要安装epel扩展源
yum install epel-release
2. (lamp)然后分别安装httpd、php、mysql
yum install -y httpd php php-mysql mysql mysql-server mysql-devel php-gd libjpeg libjpeg-devel libpng libpng-devel
3. 安装cacti net-snmp rrdtool
yum install -y cacti net-snmp net-snmp-utils rrdtool
4. 启动服务:
/etc/init.d/mysqld start
/etc/init.d/httpd start
/etc/init.d/snmpd start
5. 编辑httpd配置文件
vim /etc/httpd/conf.d/cacti.conf
把"Deny from all" 改为 "Allow from all"
/etc/init.d/httpd restart
6. 导入数据创建cacti库
mysql -uroot -e "create database cacti"
创建cacti用户
mysql -uroot -e "grant all on cacti.* to 'cacti'@'127.0.0.1' identified by 'cacti';"
导入sql文件
mysql -uroot cacti < /usr/share/doc/cacti-0.8.8b/cacti.sql
7. 编辑cacti配置文件
vim /usr/share/cacti/include/config.php 更改如下:
$database_type = "mysql";
$database_default = "cacti";
$database_name = "cacti";
$database_hostname = "127.0.0.1";
$database_username = "cacti";
$database_password = "cacti";
$database_port = "3306";
$database_ssl = false;
8. web访问cacti并安装
http://ip/cacti/
点两下“next” 和一次”Finish“ 即可
输入admin admin 登录,重新设置新的密码
9. 执行poller.php, 生成图形, 加入计划任务
/usr/bin/php /usr/share/cacti/poller.php添加cron任务
crontab -e 增加:
*/5 * * * * /usr/bin/php /usr/share/cacti/poller.php
以下10-12步骤在客户端机器上操作
10. 安装snmp
yum install -y net-snmp
11. 修改snmpd.conf
修改syslocation以及syscontact, 其中syslocation 可以写本机ip,syscontact写管理员邮箱
vim /etc/snmp/snmpd.conf
syslocation 11.20
syscontact Root 1212@1212.com
12. 启动snmp
service snmpd start
// /etc/init.d/snmpd start
13. 登录cacti管理后台,点console , 再点Device, 在右上角点”Add“
Description 写本机ip或你自定义一个名字
Hostname 写本机ip
Host Template 选ucd/net SNMP Host
SNMP Version 选Version 2
点右下角的create
点右上角的”Create Graphs for this Host“
Graph Types: 选择SNMP - Interface Statistics
在下面框中选择要监控的网卡,比如我选择eth0, 在最右侧小方块里打对勾,然后点右下角的create
(如果在这一步找不到网卡:
只需要在客户机(你要监控的机器)修改配置文件:
[root@localhost ~]# vi /etc/snmp/snmpd.conf
view systemview included .1.3.6.1.2.1.1
修改为:
view systemview included .1.3.6.1.2.1
然后重启snmpd服务;
[root@localhost ~]# /etc/init.d/snmpd restart
如果还不能显示的话,就要在服务端将刚才添加的客户端delete,删除后重新添加一遍
)
Graph Types: 再选择 Graph Template Based
在下面的框中,选择你要监控的项目,比如ucd/net - Load Average
在右侧小方块中打对勾,然后点右下角的create
14. 点左侧的Graph Trees
选中”Default Tree“
点右上角的Add
Tree Item Type 选择 ”Host“
Host 选择我们刚刚增加的那个机器ip
点右下角的create
15. 点左上角的Graphs
在左侧可以看到
Defaut Tree下面已经增加了我们刚刚添加的主机,图形一开始不会那么快出来,要等一小会才可以。
参考文章 http://www.it165.net/admin/html/201310/1956.html
第二部分:nagios安装配置:
Nagios官网 http://www.nagios.org
1. Nagios安装 - 服务端(192.168.0.11)
Centos6默认的yum源里没有nagios相关的rpm包,但是我们可以安装一个epel的扩展源:
yum install -y epel-release
然后安装nagios相关的包
yum install -y httpd nagios nagios-plugins nagios-plugins-all nrpe nagios-plugins-nrpe
设置登录nagios后台的用户和密码:htpasswd -c /etc/nagios/passwd nagiosadmin
vim /etc/nagios/nagios.cfg
nagios -v /etc/nagios/nagios.cfg 检测配置文件
启动服务:service httpd start; service nagios start
浏览器访问: http://ip/nagios
2. Nagios安装 - 客户端(192.168.0.12)
在客户端机器上
yum install -y epel-release
yum install -y nagios-plugins nagios-plugins-all nrpe nagios-plugins-nrpe
vim /etc/nagios/nrpe.cfg 找到“allowed_hosts=127.0.0.1” 改为 “allowed_hosts=127.0.0.1,192.168.0.11” 后面的ip为服务端ip;
找到” dont_blame_nrpe=0” 改为 “dont_blame_nrpe=1”
启动客户端 /etc/init.d/nrpe start
3. 监控中心(192.168.0.11)添加被监控主机(192.168.0.12)
cd /etc/nagios/conf.d/
vim 192.168.0.12.cfg //加入:
define host{
use linux-server
host_name 192.168.0.12
alias 0.12
address 192.168.0.12
}
define service{
use generic-service
host_name 192.168.0.12
service_description check_ping
check_command check_ping!100.0,20%!200.0,50%
max_check_attempts 5
normal_check_interval 1
}
define service{
use generic-service
host_name 192.168.0.12
service_description check_ssh
check_command check_ssh
max_check_attempts 5 ;当nagios检测到问题时,一共尝试检测5次都有问题才会告警,如果该数值为1,那么检测到问题立即告警
normal_check_interval 1 ;重新检测的时间间隔,单位是分钟,默认是3分钟
notification_interval 60 ;在服务出现异常后,故障一直没有解决,nagios再次对使用者发出通知的时间。
单位是分钟。如果你认为,所有的事件只需要一次通知就够了,可以把这里的选项设为0。
}
define service{
use generic-service
host_name 192.168.0.12
service_description check_http
check_command check_http
max_check_attempts 5
normal_check_interval 1
}
以上服务不依赖于客户端nrpe服务,我们可以想象,我们在自己电脑上可以使用ping或者telnet探测远程任何一台机器是否存活、是否开启某个端口或服务。
而当我们想要检测客户端上的某个具体服务的情况时,就需要借助于nrpe了,比如想知道客户端机器的负责或磁盘使用情况。
4. 继续添加服务
服务端vim /etc/nagios/objects/commands.cfg
增加:define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
继续编辑 vim /etc/nagios/conf.d/192.168.0.12.cfg
增加如下内容:define service{
use generic-service
host_name 192.168.0.12
service_description check_load
check_command check_nrpe!check_load
max_check_attempts 5
normal_check_interval 1
}
define service{
use generic-service
host_name 192.168.0.12
service_description check_disk_hda1
check_command check_nrpe!check_hda1
max_check_attempts 5
normal_check_interval 1
}
define service{
use generic-service
host_name 192.168.0.12
service_description check_disk_hda2
check_command check_nrpe!check_hda2
max_check_attempts 5
normal_check_interval 1
}
说明: check_nrpe!check_load :这里的check_nrpe就是在commands.cfg刚刚定义的,check_load是远程主机上的一个检测脚本
在远程主机上vim /etc/nagios/nrpe.cfg 搜索check_load,这行就是在服务端上要执行的脚本了,我们可以手动执行这个脚本
把check_hda1更改一下:/dev/hda1 改为 /dev/sda1
再加一行command[check_hda2]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /dev/sda2
客户端上重启一下nrpe服务: service nrpe restart
服务端也重启一下nagios服务: service nagios restart
5. 配置告警
vim /etc/nagios/objects/contacts.cfg //增加:define contact{
contact_name 123
use generic-contact
alias aming
email lishiming2009@139.com
}
define contact{
contact_name 456
use generic-contact
alias aaa
email aminglinux@139.com
}
define contactgroup{
contactgroup_name common
alias common
members 123,456
}
然后在要需要告警的服务里面加上contactgroup
define service{
use generic-service
host_name 192.168.0.12
service_description check_load
check_command check_nrpe!check_load
max_check_attempts 5
normal_check_interval 1
contact_groups common
notifications_enabled 1 ;是否开启提醒功能。1为开启,0为禁用。一般,这个选项会在主配置文件(nagios.cfg)中定义,效果相同。
notification_period 24x7 ;发送提醒的时间段。非常重要的主机(服务)我定义为7×24,一般的主机(服务)就定义为上班时间。
如果不在定义的时间段内,无论什么问题发生,都不会发送提醒。
notification_options:w,u,c,r ;这个是service的状态。w为waning, u为unknown, c为critical, r为recover(恢复了),
类似的还有一个host对应的状态:d,u,r d = 状态为DOWN, u = 状态为UNREACHABLE , r = 状态恢复为OK
需要加入到host的定义配置里。
}
参考:
调用短信接口 http://www.aminglinux.com/bbs/thread-7380-1-1.html
整合微信 http://www.aminglinux.com/bbs/thread-7917-1-1.html
6. 配置图形显示 pnp4nagios (请自行演示,课上不演示)
(1)安装
yum install pnp4nagios rrdtool
(2)配置主配置文件
vim /etc/nagios/nagios.cfg //修改如下配置
process_performance_data=1
host_perfdata_command=process-host-perfdata
service_perfdata_command=process-service-perfdata
enable_environment_macros=1
(3)修改commands.cfg
vim /etc/nagios/objects/commands.cfg //注释掉原有对process-host-perfdata和process-service-perfdata,重新定义
define command {
command_name process-service-perfdata
command_line /usr/bin/perl /usr/libexec/pnp4nagios/process_perfdata.pl
}
define command {
command_name process-host-perfdata
command_line /usr/bin/perl /usr/libexec/pnp4nagios/process_perfdata.pl -d HOSTPERFDATA
}
(4)修改配置文件templates.cfg
vim /etc/nagios/objects/templates.cfg define host {
name hosts-pnp
register 0
action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_
process_perf_data 1
}
define service {
name srv-pnp
register 0
action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$
process_perf_data 1
}
(5)修改host和service配置
vim /etc/nagios/conf.d/192.168.0.12.cfg
把 “define host{
use linux-server”
改为:
define host{
use linux-server,hosts-pnp
修改对应的service,比如
把
define service{
use generic-service
host_name 192.168.0.12
service_description check_disk_hda1
check_command check_nrpe!check_hda1
max_check_attempts 5
normal_check_interval 1
}
改为:
define service{
use generic-service,srv-pnp
host_name 192.168.0.12
service_description check_disk_hda1
check_command check_nrpe!check_hda1
max_check_attempts 5
normal_check_interval 1
}
(6) 重启和启动各个服务:
service nagios restart
service httpd restart
service npcd start
(7) 访问测试
两种访问方法:
ip/nagios/
ip/pnp4nagios/
第三部分: zabbix安装配置
1 安装zabbix
yum install -y epel-release
安装rpm包的lamp环境 yum install -y httpd mysql mysql-libs php php-mysql mysql-server php-bcmath php-gd php-mbstring
安装zabbix服务端:
yum install zabbix20 zabbix20-agent zabbix20-server zabbix20-server-mysql zabbix20-web zabbix20-web-mysql net-snmp-devel
/etc/init.d/zabbix-server start;
/etc/init.d/zabbix-agent start
/etc/init.d/httpd start;
修改一下mysql配置文件
vim /etc/my.cnf //修改或增加如下内容
[mysql]
default-character-set = utf8
[mysqld]
character_set_server = utf8
启动mysql服务
/etc/init.d/mysqld start
建库,导入数据
mysql -uroot -p -e "create database zabbix"
mysql -uroot -p --default-character-set=utf8 zabbix < /usr/share/zabbix-mysql/schema.sql
mysql -uroot -p --default-character-set=utf8 zabbix < /usr/share/zabbix-mysql/images.sql
mysql -uroot -p --default-character-set=utf8 zabbix < /usr/share/zabbix-mysql/data.sql
2.网页安装zabbix
浏览器访问 http://ip/zabbix, 默认会有“It is not safe to rely on the system‘s timezone settings ”这样的警告信息,
需要vim /etc/php.ini 设置 date.timezone=“Asia/Shanghai”点next
解决相关的报错信息,点retry (vim /etc/php.ini)
输入mysql相关信息, 首先要测试一下,如果不通过,则需要调试,测试通过后,点next
Name 写127.0.0.1,(可以自定义)点next,再点next,最后点finish
默认管理员账号为 admin:zabbix
这时会遇到“zabbix server is not running”这样的错误,需要编辑一下 /etc/zabbix/zabbix_server.conf ,配置DBUser, DBPassword
3. 接入要监控的主机
在客户端上yum install zabbix20-agent
vim /etc/zabbix_agentd.conf //更改Server=服务端ip; ServerActive=0.0.0.0:10050; Hostname=aming(自定义,但要唯一)
启动客户端 /etc/init.d/zabbix-agent start
服务端上命令行测试:zabbix_get -s 客户端ip -p10050 -k "system.hostname"
在web界面下,点”configuration” --> “host” --> 右上角点”Create Host” 其中host name, visible name自定义,可以选择groups,这里默认即可,
ip address 写入客户端ip
配置监控项目模板:点“templates”, 点add, 在弹出的小窗口中选择Template OS Linux, 然后点select, 最后点save
4.自定义templates
Zabbix自带了很多模板,模板中有很多监控项目,比如CPU、网卡、内存、进程等等。使用系统自带模板有点太多了,所以我们可以自定义模板。
点configuration 选择 templates,点右上角的create template
Template name和Visible name 自定义,Groups 选择templates, 点save
然后我们去挑选一些项目拷贝到该模板下:比如我们找到Template OS Linux 点一下items,选择我们想要的项目,然后在下面选择copy selected to … 然后点go
Group 选择templates, 找到刚才我们自定义的templates,点copy
点configuration 选择 templates可以看到新建的templates中已经有刚刚我们copy的items了
我们可以使用和上面相同的方法自定义拷贝Triggers(触发器 ),它用来设置告警的阀值,当然我们也可以自定义编辑它
监控客户端网卡流量 http://www.apelearn.com/bbs/thread-8091-1-1.html
5. 配置发邮件
yum install -y sendmail ;
mkdir -p /home/zabbix/bin
vim /home/zabbix/bin/baojing.sh //内容:
#! /bin/bash
echo "$3" |/bin/mail -s "$2" $1
chmod +x /home/zabbix/bin/baojing.sh
在zabbix_server.conf配置文件中,有参数AlertScriptsPath和ExternalScripts
AlertScriptsPath=/home/zabbix/bin/ ---用户自定义的media types脚本
ExternalScripts=/home/zabbix/bin/ ---用户自定义的检查的脚本(item)
这样才能找到你的脚本,因为你在frontend中只是输入脚本的名称,没有路径。
创建mediea types: “Administration" -->”Media types",点击右上角“Create Media Type"其中Description填"baojing” 或其它自定义名称,
Type选择"Script",Script填”baojing.sh”然后点”Save”.
创建user: “Adimistration” --> “Users”在右上角,选择”Users”,点击”Create User”, alias: test1,自定义name和lastname password:123456;group
选择guest,回到上面点一下media,type 选择baojing,send to 写要发送邮件的邮箱,点add, 最后点save
创建action: “configuration” --> actions,右上角“Create Actions”, Name自定义,我这里写”baojing”,其他默认,
然后点右侧的“Operations”下的“New”按钮,“Operation Type”选择“Send message”,“Send Message to”选择一个或多个要发送消息的用户组,
Send to Users选择我们之前新增的test1, “Send only to”选择baojing , 点一下add,最后点save
zabbix历史记录乱码问题 http://caisangzi.blog.51cto.com/6387416/1313630
zabbix图形中乱码问题 http://www.apelearn.com/bbs/thread-8090-1-1.html
zabbix参考文章
http://www.iyunv.com/thread-22959-1-1.html
http://waringid.blog.51cto.com/65148/955939/
http://www.linuxidc.com/Linux/2014-11/109909.htm
6期胡湘林同学的分享 http://xianglinhu.blog.51cto.com/5787032/d-6
自定义脚本: http://www.linuxidc.com/Linux/2013-11/92476.htm