mysql与nagios的结合使用
一、 对mysql建库建表,并测试数据
基本信息:
库名:nh_nagios
表名:nagios_alerts
[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/sh
alerts=`curl -u nagiosadmin:password -s http://192.168.5.10/nagios/cgi-bin/status.cgi?host=all | \
grep serviceTotalsPROBLEMS |\
grep -o '[[:digit:]]'`
if [ "$alerts" = "" ]
then
echo "alerts is empty"
else
echo $alerts
fi
[root@nhserver2 ~]# ./1.sh
2
测试成功
[root@nhserver2 ~]# vim show_nh_nagios_alerts.sh
#!/bin/bash
test_get_nagios_service_alerts=`curl -u nagiosadmin:password -s http://192.168.5.10/nagios/cgi-bin/status.cgi?host=all | \
grep serviceTotalsPROBLEMS |\
grep -o '[[:digit:]]'`
#此处加入判断当无报警时,设get_nagios_service_alerts=0
if [ "$test_get_nagios_service_alerts" = "" ];
then
get_nagios_service_alerts=0
else
get_nagios_service_alerts=$test_get_nagios_service_alerts
fi
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 |
| 7 | serviceTotalsPROBLEMS | 2 |
| 8 | serviceTotalsPROBLEMS | 2 |
| 9 | serviceTotalsPROBLEMS | 1 |
| 10 | serviceTotalsPROBLEMS | 1 |
| 11 | serviceTotalsPROBLEMS | 1 |
| 12 | serviceTotalsPROBLEMS | 1 |
| 13 | serviceTotalsPROBLEMS | 0 |
| 14 | serviceTotalsPROBLEMS | 0 |
| 15 | serviceTotalsPROBLEMS | 0 |
| 16 | serviceTotalsPROBLEMS | 0 |
| 17 | serviceTotalsPROBLEMS | 0 |
| 18 | serviceTotalsPROBLEMS | 0 |
| 19 | serviceTotalsPROBLEMS | 0 |
| 20 | serviceTotalsPROBLEMS | 0 |
| 21 | serviceTotalsPROBLEMS | 2 |
| 22 | serviceTotalsPROBLEMS | 2 |
| 23 | serviceTotalsPROBLEMS | 0 |
| 24 | serviceTotalsPROBLEMS | 0 |
| 25 | serviceTotalsPROBLEMS | 0 |
| 26 | serviceTotalsPROBLEMS | 2 |
| 27 | serviceTotalsPROBLEMS | 2 |
| 28 | serviceTotalsPROBLEMS | 2 |
| 29 | serviceTotalsPROBLEMS | 2 |
| 30 | serviceTotalsPROBLEMS | 2 |
+-----------+-----------------------+---------------+
30 rows in set (0.00 sec)
mysql> exit
到此,通过shell写入nagios的数据进mysql成功。后继可考虑将shell写入cron,让它每隔一分钟就写入数据进mysql中。
三、取服务状态为CRITICAL的IP地址与服务名
[root@nhserver2 ~]# curl -u nagiosadmin:password -s http://192.168.5.10/nagios/cgi-bin/status.cgi?host=all | grep CRITICAL | \
grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' > nagios.txt
[root@nhserver2 ~]# cat nagios.txt
<TD ALIGN=LEFT valign=center CLASS='statusBGCRITICAL'><A HREF='extinfo.cgi?type=2&host=192.168.5.110&service=SSH'>SSH</A></TD></TR>
<TD ALIGN=LEFT valign=center CLASS='statusBGCRITICAL'><A HREF='extinfo.cgi?type=2&host=192.168.5.110&service=check-host-alive'>check-host-alive</A></TD></TR>
[root@nhserver2 ~]# awk -F'&' '{print $2,$3}' nagios.txt
host=192.168.5.110 service=SSH'>SSH</A></TD></TR>
host=192.168.5.110 service=check-host-alive'>check-host-alive</A></TD></TR>
[root@nhserver2 ~]# awk -F'&' '{print $2,$3}' nagios.txt | sed 's#</A></TD></TR>##g'
host=192.168.5.110 service=SSH'>SSH
host=192.168.5.110 service=check-host-alive'>check-host-alive
[root@nhserver2 ~]# awk -F'&' '{print $2,$3}' nagios.txt | sed 's#</A></TD></TR>##g' | sed "s#\'>.*##g"
host=192.168.5.110 service=SSH'>SSH
host=192.168.5.110 service=check-host-alive'>check-host-alive
[root@nhserver2 ~]# awk -F'&' '{print $2,$3}' nagios.txt | sed 's#</A></TD></TR>##g' | sed "s#\'>.*##g" > nagios2.txt
[root@nhserver2 ~]# cat nagios2.txt
host=192.168.5.110 service=SSH'>SSH
host=192.168.5.110 service=check-host-alive'>check-host-alive
[root@nhserver2 ~]# sed "s/'>.*$//g" nagios2.txt
host=192.168.5.110 service=SSH
host=192.168.5.110 service=check-host-alive
四、对nagios_alerts表加入一记录生成时间的字段,并成功记录数据
#新增一生成时间字段
mysql> alter table nagios_alerts add COLUMN createtime datetime NOT NULL;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
#查看表结构
mysql> show columns from nagios_alerts;
+---------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------+------+-----+---------+----------------+
| nagios_id | int(11) | NO | PRI | NULL | auto_increment |
| nagios_name | char(50) | NO | | NULL | |
| nagios_number | int(11) | NO | | NULL | |
| createtime | datetime | NO | | NULL | |
+---------------+----------+------+-----+---------+----------------+
4 rows in set (0.04 sec)
========================================
#生成时间的shell,但在mysql中,可以使用NOW()函数来做
[root@nhserver2 ~]# var=`date "+%Y-%m-%d %H:%M:%-S"`
[root@nhserver2 ~]# echo $var
2014-03-30 21:36:53
========================================
[root@nhserver2 ~]# vim show_nh_nagios_alerts.sh
#!/bin/bash
test_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:]]'`
create_time=`date "+%Y-%m-%d%H:%M:%-S"`
if [ "$test_get_nagios_service_alerts" = "" ];
then
get_nagios_service_alerts=0
else
get_nagios_service_alerts=$test_get_nagios_service_alerts
fi
mysql -uroot -hlocalhost -p123456 <<EOF
use nh_nagios;
#select * from nagios_alerts;
INSERT INTO nagios_alerts (nagios_name,nagios_number,createtime) VALUES("serviceTotalsPROBLEMS",$get_nagios_service_alerts,NOW()); #加入生成时间
EOF
====================================
#显示表内容
[root@nhserver2 ~]# vim show_nh_nagios_alerts_mysql.sh
#!/bin/bash
mysql -uroot -hlocalhost -p123456 <<EOF
use nh_nagios;
select * from nagios_alerts;
EOF
mysql与nagios的结合使用的更多相关文章
- Nagios 里面监控MySQL事务一直RUNNING没有结束的报警Shell脚本 (转)
序言: 业务报警订单提交异常,页面一直没有反应,排查后是事务没有提交或者回滚导致,想到如果及时监控事务的运行状态报警出来,那么就可以及时排查出问题所在,方便运营处理,所以自己就弄了一个s ...
- nagios 安装和配置(含有nrpe结束)所有 (两)
二.ndoutils 安装: 1.mysql安装(若未安装) #apt-get install mysql-servermysql-client 2.DBI安装(若未安装) #cd /usr/loca ...
- Nagios数据存储插件NDOUtils部署和测试
1. 概述 NDOUTILS,Nagios Data Output Utils,Nagios数据输出工具,允许用户从Nagios导出状态和事件信息到数据库中,便于以后的检索和加工 它包括几个部分: N ...
- awk sed 总结
Awk总结笔记 介绍 90年代 new awk :nawk Linux 的是gawk 我们简化awk 用法 # awk [options ] ‘scripts’ file1 file2 .... # ...
- linux 运维工程师发展路线
linux运维发展常见的就是下面两条路线:第一条:运维应用-->系统架构-->运维开发-->系统开发第二条:运维应用-->应用dba-->架构dba-->开发DBA ...
- Linux运维工程师是什么鬼?
第一部分:定义 运维工程师,字面理解运行维护. linux运维即linux运维工程师,集合网络.系统.数据库.开发.安全工作于一身的“复合性人才”. 除了传统IT运维部分,运维人员还是管理制度.规 ...
- Linux系统的用户和用户组管理
一.用户账户管理 Linux/Unix是一个用户.多任务的操作系统:在讲Linux账号及账户组管理之前,先简单了解一下多用户.多任务操作系统的基本概念. Linux的单用户多任务 在Linux下,当你 ...
- centos shell编程6一些工作中实践脚本 nagios监控脚本 自定义zabbix脚本 mysql备份脚本 zabbix错误日志 直接送给bc做计算 gzip innobackupex/Xtrabackup 第四十节课
centos shell编程6一些工作中实践脚本 nagios监控脚本 自定义zabbix脚本 mysql备份脚本 zabbix错误日志 直接送给bc做计算 gzip innobacku ...
- 将Nagios监控信息存入Mysql
一.NDOUtils安装需求: nagios:安装方法:http://www.cnblogs.com/Richardzhu/p/3340638.html mysql:源码安装方法:http://www ...
随机推荐
- ssh远程登录命令简单实例
ssh远程登录命令简单实例 ssh命令用于远程登录上Linux主机. 常用格式:ssh [-l login_name] [-p port] [user@]hostname 更详细的可以用ssh -h查 ...
- Zabbix实战-简易教程--宏变量(Macro)
一.概述 Zabbix支持许多在多种情况下使用的宏.宏是一个变量,由如下特殊语法标识:MACRO 有效地使用宏可以节省时间,并使Zabbix变地更加高效. 在一个的典型用途中,宏可以用于模板中.因此, ...
- Erlang内存吃紧之解决思路
首先使用erlang:memory()确定是哪个部分内存吃紧,根据输出的内容,比对内存占用大小,有针对性地进行分析.在erlang系统里内存的单位为word,通过erlang:system_info( ...
- sqlalchemy-数据库操作
import datetime from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarat ...
- Java集合源码分析(四)HashMap
一.HashMap简介 1.1.HashMap概述 HashMap是基于哈希表的Map接口实现的,它存储的是内容是键值对<key,value>映射.此类不保证映射的顺序,假定哈希函数将元素 ...
- [51nod1407]与与与与
有n个整数,问从他们中取出若干个数字相与之后结果是0的有多少组. 答案比较大,输出对于 1,000,000,007 (1e9+7)取模后的结果. Input 第一行输入一个整数n.(1<=n&l ...
- [bzoj3910] 火车
一开始看错题了... 求经过的道路数量就求个lca,把路径上的点置为已经过的话,可以写一个并查集,把某个点往上连续已经过的一段点并起来. #include<cstdio> #include ...
- Matrix Chain Multiplication(表达式求值用栈操作)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...
- DataURL与File,Blob,canvas对象之间的互相转换的Javascript
canvas转换为dataURL (从canvas获取dataURL) var dataurl = canvas.toDataURL('image/png'); var dataurl2 = canv ...
- .31-浅析webpack源码之doResolve事件流(2)
放个流程图: 这里也放一下request对象内容,这节完事后如下(把vue-cli的package.json也复制过来了): /* { context: { issuer: '', compiler: ...