mysql与nagios的结合使用
一、 对mysql建库建表,并测试数据
[root@nhserver2 ~]# mysql -u root -p
Enter
password:123456
mysql> show
databases;
+--------------------+
|
Database
|
+--------------------+
| information_schema |
|
cactidb
|
|
mysql
|
|
test
|
+--------------------+
5 rows in set (0.00
sec)
mysql> create database nh_nagios;
mysql>
show databases;
+--------------------+
|
Database
|
+--------------------+
| information_schema |
|
cactidb
|
|
mysql
|
|
nh_nagios
|
|
test
|
+--------------------+
5 rows in set (0.00
sec)
mysql> use nh_nagios
Database
changed
mysql> create table
nagios_alerts
-> (
-> nagios_id int not null
auto_increment,
-> nagios_name
char(50) not null,
-> nagios_number
int not null,
-> primary
key(nagios_id)
-> )engine=innod default
charset=utf8 auto_increment=1;
Query OK, 0 rows affected, 2
warnings (0.00 sec)
mysql> show
tables;
+---------------------+
| Tables_in_nh_nagios
|
+---------------------+
|
nagios_alerts
|
+---------------------+
1 row in set (0.00
sec)
mysql> INSERT INTO nagios_alerts
(nagios_name,nagios_number)
VALUES("serviceTotalsPROBLEMS",2);
Query OK, 1 row affected (0.00
sec)
mysql> select * from
nagios_alerts;
+-----------+-----------------------+---------------+
|
nagios_id |
nagios_name
| nagios_number
|
+-----------+-----------------------+---------------+
|
1 | serviceTotalsPROBLEMS
|
2 |
+-----------+-----------------------+---------------+
1
row in set (0.00 sec)
mysql> INSERT INTO nagios_alerts
(nagios_name,nagios_number)
VALUES("serviceTotalsPROBLEMS",10);
Query OK, 1 row affected
(0.00 sec)
mysql> select * from
nagios_alerts;
+-----------+-----------------------+---------------+
|
nagios_id |
nagios_name
| nagios_number
|
+-----------+-----------------------+---------------+
|
1 | serviceTotalsPROBLEMS
|
2 |
| 2 |
serviceTotalsPROBLEMS
|
10 |
+-----------+-----------------------+---------------+
2
rows in set (0.00 sec)
mysql> exit
二、在shell中执行插入数据进mysql的测试
[root@nhserver2 ~]# vim
show_nh_nagios_alerts.sh
#!/bin/bash
mysql -uroot -hlocalhost
-p123456 <<EOF
use nh_nagios;
#select
* from nagios_alerts;
INSERT INTO nagios_alerts (nagios_name,nagios_number)
VALUES("serviceTotalsPROBLEMS",30);
EOF
[root@nhserver2
~]# chmod 700 show_nh_nagios_alerts.sh
[root@nhserver2 ~]#
./show_nh_nagios_alerts.sh
nagios_id
nagios_name
nagios_number
1
serviceTotalsPROBLEMS
2
2
serviceTotalsPROBLEMS 10
[root@nhserver2 ~]#
mysql -u root -p
Enter password:123456
mysql> use
nh_nagios
Reading table information for completion of table and
column names
You can turn off this feature to get a quicker
startup with -A
Database changed
mysql> select * from
nagios_alerts;
+-----------+-----------------------+---------------+
|
nagios_id |
nagios_name
| nagios_number
|
+-----------+-----------------------+---------------+
|
1 | serviceTotalsPROBLEMS
|
2 |
| 2 |
serviceTotalsPROBLEMS
|
10 |
| 3 |
serviceTotalsPROBLEMS
|
30 |
+-----------+-----------------------+---------------+
3
rows in set (0.00 sec)
mysql>
exit
使用语句自动取nagios_number的数据,即nagios内serviceTotalsPROBLEMS的值
[root@nhserver2 ~]# vim
1.sh
#!/bin/bash
get_nagios_service_alerts=`curl -u
nagiosadmin:skylink -s
http://192.168.5.10/nagios/cgi-bin/status.cgi?host=all | \
grep
serviceTotalsPROBLEMS |\
grep -o ‘[[:digit:]]‘`
echo
$get_nagios_service_alerts
[root@nhserver2 ~]#
./1.sh
2
测试成功
[root@nhserver2 ~]# vim
show_nh_nagios_alerts.sh
#!/bin/bash
get_nagios_service_alerts=`curl
-u nagiosadmin:skylink -s
http://192.168.5.10/nagios/cgi-bin/status.cgi?host=all | \
grep
serviceTotalsPROBLEMS |\
grep -o ‘[[:digit:]]‘`
mysql
-uroot -hlocalhost -p123456
<<EOF
use
nh_nagios;
#select *
from nagios_alerts;
INSERT INTO nagios_alerts (nagios_name,nagios_number)
VALUES("serviceTotalsPROBLEMS",$get_nagios_service_alerts);
EOF
写入3条数据进mysql中
[root@nhserver2
~]# ./show_nh_nagios_alerts.sh
[root@nhserver2 ~]#
./show_nh_nagios_alerts.sh
[root@nhserver2 ~]#
./show_nh_nagios_alerts.sh
[root@nhserver2 ~]# mysql -u root
-p
Enter password:123456
mysql> use
nh_nagios
Reading table information for completion of table and
column names
You can turn off this feature to get a quicker
startup with -A
Database changed
mysql> select *
from
nagios_alerts;
+-----------+-----------------------+---------------+
|
nagios_id |
nagios_name
| nagios_number
|
+-----------+-----------------------+---------------+
|
1 | serviceTotalsPROBLEMS
|
2 |
| 2 |
serviceTotalsPROBLEMS
|
10 |
| 3 |
serviceTotalsPROBLEMS
|
30 |
| 4 |
serviceTotalsPROBLEMS
|
2 |
| 5 |
serviceTotalsPROBLEMS
|
2 |
| 6 |
serviceTotalsPROBLEMS
|
2 |
+-----------+-----------------------+---------------+
6
rows in set (0.00 sec)
mysql>
exit
到此,通过shell写入nagios的数据进mysql成功。后继可考虑将shell写入cron,让它每隔一分钟就写入数据进mysql中。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。