nagios圖形介面還是比較麻煩的,下面以mysql為例來說一下。監控mysql的外掛程式比較多,在這裡瞭解方法,而不是死搬硬套。
一,安裝pnp4nagios
[root@localhost objects]# yum install pnp4nagios rrdtool
如果找不到包換epel源,部落格裡面有搜一下。關於nagios的安裝,請參考:nagios 安裝配置 詳解
二,配置nagios
1,修改nagios.cfg
[root@localhost objects]# vim /etc/nagios/nagios.cfg //修改以下內容
process_performance_data=1 //由0改為1
host_perfdata_command=process-host-perfdata //前面的注釋拿掉
service_perfdata_command=process-service-perfdata //注釋拿掉
enable_environment_macros=1 //如果有注釋拿掉
2,修改commands.cfg
注釋掉原有對process-host-perfdata和process-service-perfdata,重新定義
[root@localhost objects]# vim /etc/nagios/objects/commands.cfg
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
添加以下內容
[root@localhost objects]# 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,修改伺服器設定檔localhost.cfg
[root@localhost objects]# vim /etc/nagios/objects/localhost.cfg
define host{
use linux-server,hosts-pnp
host_name localhost
alias localhost
address 127.0.0.1
}
define service{
use local-service,srv-pnp
host_name localhost
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
上面只是例舉了一個,你可以都加一下。
三,pnp4nagios配置
在網上好多關於pnp4nagios的文章,都是要把mv misccommands.cfg-sample misccommands.cfg 組建組態檔案,我是yum安裝的,安裝目錄下根本沒有sample這樣的東西
[root@localhost objects]# tree /etc/pnp4nagios/
/etc/pnp4nagios/
├── background.pdf
├── check_commands
│ ├── check_all_local_disks.cfg
│ ├── check_nrpe.cfg
│ └── check_nwstat.cfg
├── config.php
├── misccommands.cfg
├── nagios.cfg
├── npcd.cfg
├── pages
│ └── web_traffic.cfg
├── pnp4nagios_release
├── process_perfdata.cfg
└── rra.cfg
所在我根本沒有配置任何東西。
四,重啟nagios,啟動npcd
[root@localhost pnp4nagios]# /etc/init.d/npcd start
[root@localhost pnp4nagios]# /etc/init.d/nagios restart
五,nginx配置
配置nginx的時候,配置挺頭痛的,nagios的根目錄是有php的,pnp4nagios是放在nagios的根目錄下面,pnp4nagios也是php的,配置不好容易錯亂的。
server
{
listen 80;
server_name nagios.xxxx.com;
index index.php;
root /usr/share/nagios/html;
location ~ .*\.(php|php5)?$ { //nagios根目錄的php
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { //nagios的圖片重寫
rewrite ^/nagios/images/(.*) /images/$1 break;
rewrite ^/pnp4nagios/index.php/(.*) /pnp4nagios/$1 break;
expires 30d;
}
location ~ .*\.(js|css)?$ { //nagios的js,css重寫
rewrite ^/nagios/stylesheets/(.*) /stylesheets/$1 break;
rewrite ^/nagios/js/(.*) /js/$1 break;
rewrite ^/pnp4nagios/index.php/(.*) /pnp4nagios/$1 break;
expires 1h;
}
location ~ .*\.cgi$ { //nagios的主程式是perl的,perl的cgi重寫
root /usr/lib64/nagios/cgi-bin;
rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.cgi;
include fastcgi.conf;
}
location ~ ^(/pnp4nagios.*\.php)(.*)$ { //pnp4nagios的php重寫
rewrite ^/pnp4nagios/index.php/index.php/(.*)$ /pnp4nagios/index.php/$1; break;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
在這裡有一個問題就是http://nagios.xxxx.com/pnp4nagios/index.php/index.php/graph?host=localhost&srv=PING在這裡有二個index.php,這個是php的程式加上去的,我並沒有改。同一個頁面有二種連結。
由nagios的監控頁面,跳轉到pnp4nagios監控頁面的串連是
http://nagios.xxxx.com/pnp4nagios/index.php/graph?host=localhost&srv=PING
點php4nagios監控頁面中的串連然後就會多加一個index.php,
http://nagios.xxxx.com/pnp4nagios/index.php/index.php/graph?host=localhost&srv=PING
這也是上面我為什麼要加這一句rewrite ^/pnp4nagios/index.php/index.php/(.*)$ /pnp4nagios/index.php/$1; break;的原因
二,安裝perl mysql擴充
# yum install perl-Class-DBI-mysql
三,check_mysqld.php和check_mysqld.pl
1,下載
http://exchange.nagios.org/components/com_mtree/attachment.php?link_id=174&cf_id=30
http://exchange.nagios.org/components/com_mtree/attachment.php?link_id=174&cf_id=36
2,修改check_mysqld.php和check_mysqld.pl,本機是64位,根本沒有/usr/lib/nagios,所以要把檔案中的lib改成lib64
3,copy和修改檔案許可權
# cp check_mysqld.pl /usr/lib64/nagios/plugins
# chmod 755 /usr/lib64/nagios/plugins/check_mysqld.pl
# chgrp nagios /usr/lib64/nagios/plugins/check_mysqld.pl
# cp check_mysqld.php /usr/share/nagios/html/pnp4nagios/templates.dist
四,nagios配置
1,修改command.cfg
# vim /etc/nagios/objects/commands.cfg
define command{
command_name check_mysqld
command_line $USER1$/check_mysqld.pl -H $HOSTADDRESS$ -u nagios -p $USER7$ -a uptime,threads_connected,questions,slow_queries,open_tables -w ',,,,' -c ',,,,' -A $USER21$
}
2,修改resource.cfg
# vim /etc/nagios/objects/resource.cfg
$USER7$=nagios
$USER21$='com_select,com_update,com_insert,com_insert_select,
com_commit,com_delete,com_rollback,aborted_clients,
aborted_connects,binlog_cache_disk_use,binlog_cache_use,
bytes_received,bytes_sent,connections,created_tmp_disk_tables,
created_tmp_files,created_tmp_tables,delayed_errors,
delayed_insert_threads,delayed_writes,handler_update,handler_write,
handler_delete,handler_read_first,handler_read_key,
handler_read_next,handler_read_prev,handler_read_rnd,
handler_read_rnd_next,key_blocks_not_flushed,
key_blocks_unused,key_blocks_used,key_read_requests,key_reads,
key_write_requests,key_writes,max_used_connections,
not_flushed_delayed_rows,open_files,open_streams,open_tables,
opened_tables,prepared_stmt_count,qcache_free_blocks,
qcache_free_memory,qcache_hits,qcache_inserts,
qcache_lowmem_prunes,qcache_not_cached,
qcache_queries_in_cache,qcache_total_blocks,questions,
select_full_join,select_rangle_check,slow_launch_threads,
slow_queries,table_locks_immediate,table_locks_waited,
threads_cached,threads_connected,threads_created,
threads_running'
在這裡要注意,$USER21$後面的內容要在一行。
3,修改localhost.cfg
# vim /etc/nagios/objects/localhost.cfg
define service {
use local-service,srv-pnp //srv-pnp換成你自訂的
host_name localhost
service_description MYSQLD
check_command check_mysqld!localhost!nagios!nagios
}
4,pnp4nagios中增加check_mysqld.cfg
# vim /etc/pnp4nagios/check_commands/check_mysqld.cfg
DATATYPE = COUNTER
五,重啟nagios
# /etc/init.d/npcd restart
# /etc/init.d/nrpe restart
# /etc/init.d/nagios restart
六,查看結果