nagios插件之登陆防火墙实现session监控
ssh_firewall_session.sh -- 登陆防火墙并运行dis session statistics
firewall_check_sessions.c -- 调用上面脚本。过滤出sessioin的数值
运行:./firewall_check_sessions ssh_firewall_session.sh 192.168.0.1
vi ssh_firewall_session.sh
#!/usr/bin/expect -f #set port 22
set user xxxxxx
set host [lindex $argv 0 0]
set password xxxxxx
set timeout 30 expect "*assword:*"
send "$password\r" expect "*FW*"
send "display session statistics\r" expect "*FW*"
send "quit"
vi firewall_check_sessions.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <regex.h>
#include <string.h> #define OK 0
#define WARNING 1
#define CRITICAL 2
#define UNKNOWN 3 #define LEN 1000L
#define MIN_LEN 32L //#define TCL_CMD "/home/weihu/tcl/"
#define TCL_CMD "/usr/local/nagios/libexec/" int exitstatus=OK;
char *exit_status[4]={"OK","WARNING","CRITICAL","UNKNOWN"}; char status_information[LEN];
char performance_data[LEN]; //current session
char cur_session[MIN_LEN]={0};
char tcp_session[MIN_LEN]={0};
char half_open[MIN_LEN]={0};
char half_close[MIN_LEN]={0};
char udp_session[MIN_LEN]={0};
char icmp_session[MIN_LEN]={0};
char rawip_session[MIN_LEN]={0}; //new create session per second
char cur_new_session[MIN_LEN]={0};
char tcp_new_session[MIN_LEN]={0};
char udp_new_session[MIN_LEN]={0};
char icmp_new_session[MIN_LEN]={0};
char rawip_new_session[MIN_LEN]={0}; int parse_status(char *sh_cmd,char *active_status,char *active_ip_addr) {
int ret;
FILE *fp;
char readbuf[LEN]; char *p,*str; int line=0;
int mark1=0;
int mark2=0; /*
char tmp1[LEN];
char tmp2[LEN];
char tmp3[LEN];
char tmp4[LEN];
*/ int mark=0;
char tmp1[MIN_LEN];
char tmp2[MIN_LEN]; regex_t reg;
int cflags=REG_EXTENDED;
const char *pattern="^Session establishment rate";
regmatch_t pmatch[1];
int nmatch=1; ret=regcomp(®,pattern,cflags);
if(ret!=0) {
fprintf(stderr,"regcomp() error.\n");
} // fp=popen("/home/neo/check_log/tcl/auto_ssh.sh","r");
fp=popen(sh_cmd,"r");
if(fp==NULL) {
fprintf(stderr,"popen() error. ");
exitstatus=CRITICAL;
printf("%s: - %s | %s\n",exit_status[exitstatus],status_information,performance_data);
exit(exitstatus);
} while(fgets(readbuf,LEN,fp)!=NULL) {
line++;
// printf("line=%d,readbuf=%s",line,readbuf);
if(strstr(readbuf,"Current session")) {
sscanf(readbuf,"Current session(s):%s",cur_session);
// printf("cur_session=%s\n",cur_session);
} if(strstr(readbuf,"TCP session")) {
sscanf(readbuf," Current TCP session(s): %s",tcp_session);
// printf("tcp_session=%s\n",tcp_session);
} if(strstr(readbuf,"Half-Open")) {
sscanf(readbuf," Half-Open: %s Half-Close: %s",half_open,half_close);
// printf("half_open=%s,half_close=%s\n",half_open,half_close);
} if(strstr(readbuf,"UDP session")) {
sscanf(readbuf," Current UDP session(s): %s",udp_session);
// printf("udp_session=%s\n",udp_session);
} if(strstr(readbuf,"ICMP session")) {
sscanf(readbuf," Current ICMP session(s): %s",icmp_session);
// printf("icmp_session=%s\n",icmp_session);
} if(strstr(readbuf,"RAWIP session")) {
sscanf(readbuf," Current RAWIP session(s): %s",rawip_session);
// printf("rawip_session=%s\n",rawip_session);
// break;
} if(strstr(readbuf,"Session establishment rate")) {
mark++;
}
if(mark==1) {
ret=regexec(®,readbuf,nmatch,pmatch,0);
if(ret==0) {
// printf("-----------readbuf=%s",readbuf); sscanf(readbuf,"Session establishment rate: %[^/s]",cur_new_session);
// printf("cur_new_session=%s\n",cur_new_session);
}
} if(strstr(readbuf,"TCP Session establishment rate")) {
sscanf(readbuf," TCP Session establishment rate: %[^/s]",tcp_new_session);
// printf("tcp_new_session=%s\n",tcp_new_session);
} if(strstr(readbuf,"UDP Session establishment rate")) {
sscanf(readbuf," UDP Session establishment rate: %[^/s]",udp_new_session);
// printf("udp_new_session=%s\n",udp_new_session);
} if(strstr(readbuf,"ICMP Session establishment rate")) {
sscanf(readbuf," ICMP Session establishment rate: %[^/s]",icmp_new_session);
// printf("icmp_new_session=%s\n",icmp_new_session);
} if(strstr(readbuf,"RAWIP Session establishment rate")) {
sscanf(readbuf," RAWIP Session establishment rate: %[^/s]",rawip_new_session);
// printf("rawip_new_session=%s\n",rawip_new_session);
}
/*
if(line==3) {
for(p=strtok(readbuf," ");p;p=strtok(NULL," ")) {
// str=p; //Sun
mark1++; if(mark1==2) {
// printf("p=%s\n",p); strcpy(active_status,p);
// printf("active_status=%s\n",active_status);
}
}
} if(line==4) {
for(p=strtok(readbuf," ");p;p=strtok(NULL," /")) {
mark2++; if(mark2==2) {
// printf("p=%s\n",p); strcpy(active_ip_addr,p);
// printf("active_ip_addr=%s\n",active_ip_addr);
}
} break;
}
*/
} // printf("line=%d\n",line); ret=pclose(fp);
if(ret==-1) {
fprintf(stderr,"popen() error.\n");
return -1;
} return 0;
} int main(int argc, char *argv[]) {
int ret;
char sh_cmd[LEN]; char active_status[LEN];
char active_ip_addr[LEN]; if(argc<=1) {
printf("%s %s\n",argv[0],"auto_ssh_firewall_dis_session.sh + ip");
exit(-1);
} sprintf(sh_cmd,"%s%s %s",TCL_CMD,argv[1],argv[2]);
// printf("sh_cmd=%s\n",sh_cmd); ret=parse_status(sh_cmd,active_status,active_ip_addr);
if(ret!=0) {
fprintf(stderr,"parse_status() error.\n"); sprintf(status_information,"cur_all_session=%s, cur_all_session_new=%s, tcp_all_session=%s, tcp_all_session_new=%s, tcp_half_open=%s, tcp_half_close=%s, udp_session=%s, udp_session_new=%s, icmp_session=%s, icmp_session_new=%s, rawip_session=%s, rawip_session_new=%s", cur_session, cur_new_session, tcp_session, tcp_new_session, half_open, half_close, udp_session, udp_new_session, icmp_session, icmp_new_session, rawip_session, rawip_new_session); sprintf(performance_data,"cur_all_session=%s;;;; cur_all_session_new=%s;;;; tcp_all_session=%s;;;; tcp_all_session_new=%s;;;; tcp_half_open=%s;;;; tcp_half_close=%s;;;; udp_session=%s;;;; udp_session_new=%s;;;; icmp_session=%s;;;; icmp_session_new=%s;;;; rawip_session=%s;;;; rawip_session_new=%s;;;;", cur_session, cur_new_session, tcp_session, tcp_new_session, half_open, half_close, udp_session, udp_new_session, icmp_session, icmp_new_session, rawip_session, rawip_new_session); exitstatus=CRITICAL;
printf("%s - %s | %s\n", exit_status[exitstatus], status_information, performance_data); return exitstatus;
} /*
// printf("active_status=%s\n",active_status);
// printf("active_ip_addr=%s\n",active_ip_addr); // if(atoi(ping_avg)<200 && atoi(loss_packet)==0) {
if(atoi(ping_avg)<200 && atoi(loss_packet_int)==0) {
exitstatus=OK;
}
// else if(atoi(ping_avg)>=200 && atoi(ping_avg)<500 || atoi(loss_packet)>=10 && atoi(loss_packet)<=50) {
else if(atoi(ping_avg)>=200 && atoi(ping_avg)<500 || atoi(loss_packet_int)>=10 && atoi(loss_packet_int)<=50) {
exitstatus=WARNING;
}
// else if(atoi(ping_avg)>=500 || atoi(loss_packet)>50) {
else if(atoi(ping_avg)>=500 || atoi(loss_packet_int)>50) {
exitstatus=CRITICAL;
}
else {
exitstatus=CRITICAL;
} // sprintf(status_information,"rta %s%s, loss %s",ping_avg,ping_unit,loss_packet);
sprintf(status_information,"rta %s%s, loss %s%%",ping_avg,ping_unit,loss_packet_int); // sprintf(performance_data,"rta=%s%s;200.000;500.000;0; pl=%s;40;80;; rtmax=%s%s;;;; rtmin=%s%s;;;;",ping_avg,ping_unit,loss_packet,ping_max,ping_unit,ping_min,ping_unit);
sprintf(performance_data,"rta=%s%s;200.000;500.000;0; pl=%s%%;40;80;; rtmax=%s%s;;;; rtmin=%s%s;;;;",ping_avg,ping_unit,loss_packet_int,ping_max,ping_unit,ping_min,ping_unit); //|rta=0.056ms;200.000;500.000;0; pl=0%;40;80;; rtmax=0.084ms;;;; rtmin=0.029ms;;;; if(strstr(argv[1],"80_49")) {
printf("%s - 192.20.198.121: %s | %s\n",exit_status[exitstatus],status_information,performance_data);
} if(strstr(argv[1],"80_50")) {
printf("%s - 192.20.198.181: %s | %s\n",exit_status[exitstatus],status_information,performance_data);
} return exitstatus;
*/ sprintf(status_information,"cur_all_session=%s, cur_all_session_new=%s, tcp_all_session=%s, tcp_all_session_new=%s, tcp_half_open=%s, tcp_half_close=%s, udp_session=%s, udp_session_new=%s, icmp_session=%s, icmp_session_new=%s, rawip_session=%s, rawip_session_new=%s", cur_session, cur_new_session, tcp_session, tcp_new_session, half_open, half_close, udp_session, udp_new_session, icmp_session, icmp_new_session, rawip_session, rawip_new_session); sprintf(performance_data,"cur_all_session=%s;;;; cur_all_session_new=%s;;;; tcp_all_session=%s;;;; tcp_all_session_new=%s;;;; tcp_half_open=%s;;;; tcp_half_close=%s;;;; udp_session=%s;;;; udp_session_new=%s;;;; icmp_session=%s;;;; icmp_session_new=%s;;;; rawip_session=%s;;;; rawip_session_new=%s;;;;", cur_session, cur_new_session, tcp_session, tcp_new_session, half_open, half_close, udp_session, udp_new_session, icmp_session, icmp_new_session, rawip_session, rawip_new_session); printf("%s - %s | %s\n", exit_status[exitstatus], status_information, performance_data); return exitstatus;
}
nagios插件之登陆防火墙实现session监控的更多相关文章
- nagios插件之登陆SBC监控电话数
运行:sbc_calls_status_new auto_ssh_sbc_10_17.sh | auto_ssh_sbc_11_17.sh vi sbc_calls_status_new.c #inc ...
- 关于nagios系统下使用shell脚本自定义监控插件的编写以及没有实时监控图的问题
关于nagios系统下shell自定义监控插件的编写.脚本规范以及没有实时监控图的问题的解决办法 在自已编写监控插件之前我们首先需要对nagios监控原理有一定的了解 Nagios的功能是监控服务和主 ...
- 用python 写一个nagios插件 监控http内容(转载)
nagios自带的http-check插件主要是检测地址url是否可以访问,在web+中间件的架构中容易出现url能访问,但是后台中间件拓机的情况,因为最近在自学python,所以写了个脚本检测ur ...
- Nagios的安装配置与应用之五监控远程Linux服务器
本文出自 “曹坏水” 博客,请务必保留此出处http://cao2012.blog.51cto.com/366908/1132113 NRPE是Nagios的一个功能扩展,它可在远程Linux和UNI ...
- nagios 实现Mysql 主从同步状态的监控
一.系统环境 主机名 IP nagios 192.168.15.111 mysql_s 192.168.15.21 二.操作步骤 2.1 mysql_s端的配置 2.1.1 编写check_mysql ...
- 使用nagios插件 check_mysql_health 过程中遇到的error
使用nagios插件 check_mysql_health 过程中遇到的error 1.如果在运行监控mysql插件的时候遇到了error安装以下依赖包就可以解决: yum install perl- ...
- Eclipse下使用GDT插件无法登陆GAE & GDT无法上传JAVA代码
今天更新github主页的过程中,想使用GAE部署一个Java Web服务来更好的支持网站动态性(关键是利用了免费的GAE资源),结果遇到了2个大问题. 1.GDT插件无法登陆GAE账户 错误1:登陆 ...
- 开发人员福利!ChromeSnifferPlus 插件正式登陆 Chrome Web Store
今天(2014-10-30)下午,ChromeSnifferPlus 插件正式登陆 Chrome Web Store. 在线安装地址: https://chrome.google.com/websto ...
- nagios 插件ndoutils 安装配置
nagios 插件ndoutils 安装配置 原文地址:http://www.cnblogs.com/caoguo/p/5022645.html # Nagios install ndoutils # ...
随机推荐
- 搭建Mysql双机热备 (主从同步)
准备两台centos7主机:10.0.18.132 master 10.0.18.136 slave 先把selinux关闭,iptables关闭 或者添加端口 132 master安装好Mysq ...
- PostgreSQL Replication之第二章 理解PostgreSQL的事务日志(4)
2.4 调整检查点和XLOG 目前为止,这一章已经提供深入洞察PostgreSQL如何写入数据,一般来说,XLOG是用来干什么的.考虑到这方面的知识,我们现在可以继续并学习我们能做些什么来使我们的数据 ...
- sql 知识点
1 聚集索引和非聚集索引的区别 聚集索引是指 表中存储的数据按照索引的顺序来存储,数据检索效率高,但是对数据更新影响较大:非聚集索引是数据存储在一个地方,索引存储在另一个地方:索引带有指针,指向数据的 ...
- php 扩展模块安装过程
安装php模块,不需要重新编译php,可以动态扩展php模块. 以安装msgpack模块为例:(此处php5.6是编译安装的) 1.下载msgpack模块源码包 # wget http://pecl. ...
- vue的鼠标移入和移出
vue的鼠标移入和移出 需求(鼠标到预约二维码显示,预约添加背景色) 实现 <!--html部分--> <ul class="person_list"> / ...
- [NOIP2016普及组]魔法阵
题目:洛谷P2119.Vijos P2012.codevs5624. 题目大意:有n件物品,每件物品有个魔法值.要求组成魔法阵(Xa,Xb,Xc,Xd),该魔法阵要满足Xa<Xb<Xc&l ...
- java 基于 bootstrap_datagrid 分页
1.首先引入datagrid js ,css $("#datagrid").bootstrap_datagrid({ url : "<%=path%>/us ...
- 紫书 例题 10-26 UVa 11440(欧拉函数+数论)
这里用到了一些数论知识 首先素因子都大于M等价与M! 互质 然后又因为当k与M!互质且k>M!时当且仅当k mod M! 与M!互质(欧几里得算法的原理) 又因为N>=M, 所以N!为M! ...
- [ZJOI2012]旅游(树的直径)
[ZJOI2012]旅游 题目描述 到了难得的暑假,为了庆祝小白在数学考试中取得的优异成绩,小蓝决定带小白出去旅游~~ 经过一番抉择,两人决定将T国作为他们的目的地.T国的国土可以用一个凸N边形来表示 ...
- Eclipse反编译插件 Enhanced Class Decompiler
因为jar包中的源码都是经过反编译的,所以需要安装插件才能查看到源码,此处介绍的是 Enhanced Class Decompiler 插件. 打开Eclipse,Help --> Eclips ...