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 <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];

//save 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};

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];

//	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(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_session=%s, tcp_session=%s, half_open=%s, half_close=%s udp_session=%s icmp_session=%s, rawip_session=%s", cur_session, tcp_session, half_open, half_close, udp_session, icmp_session, rawip_session);

		sprintf(performance_data,"cur_session=%s;;;; tcp_session=%s;;;; half_open=%s;;;; half_close=%s;;;; udp_session=%s;;;; icmp_session=%s;;;; rawip_session=%s;;;;", cur_session, tcp_session, half_open, half_close, udp_session, icmp_session, rawip_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, tcp_all_session=%s, tcp_half_open=%s, tcp_half_close=%s udp_session=%s icmp_session=%s, rawip_session=%s", cur_session, tcp_session, half_open, half_close, udp_session, icmp_session, rawip_session);

	sprintf(performance_data,"cur_all_session=%s;;;; tcp_all_session=%s;;;; tcp_half_open=%s;;;; tcp_half_close=%s;;;; udp_session=%s;;;; icmp_session=%s;;;; rawip_session=%s;;;;", cur_session, tcp_session, half_open, half_close, udp_session, icmp_session, rawip_session); 

        printf("%s - %s | %s\n", exit_status[exitstatus], status_information, performance_data);

	return exitstatus;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-06-03 14:00:47

nagios插件之登陆防火墙实现session监控的相关文章

nagios插件之登陆路由器实现ping监控

router_check_apn_ping.c #include <stdio.h> #include <stdlib.h> #include <string.h> #define OK 0 #define WARNING 1 #define CRITICAL 2 #define UNKNOWN 3 #define LEN 1000 //#define TCL_CMD "/home/weihu/tcl/" #define TCL_CMD "

nagios插件之登陆SBC监控电话数

执行:sbc_calls_status_new auto_ssh_sbc_10_17.sh | auto_ssh_sbc_11_17.sh vi sbc_calls_status_new.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #define OK 0 #define WARNING 1 #define CRITICAL 2 #define

nagios插件之系统打开文件数监控

vi check_open_file.c (仅限linux系统使用) #include <stdio.h> #include <stdlib.h> #include <string.h> #define OK 0 #define WARNING 1 #define CRITICAL 2 #define UNKNOWN 3 #define LEN 1000 #define FILE_NR "/proc/sys/fs/file-nr" int exits

关于nagios系统下使用shell脚本自定义监控插件的编写以及没有实时监控图的问题

关于nagios系统下shell自定义监控插件的编写.脚本规范以及没有实时监控图的问题的解决办法 在自已编写监控插件之前我们首先需要对nagios监控原理有一定的了解 Nagios的功能是监控服务和主机,但是他自身并不包括这部分功能,所有的监控.检测功能都是通过各种插件来完成的. 启动Nagios后,它会周期性的自动调用插件去检测服务器状态,同时Nagios会维持一个队列,所有插件返回来的状态信息都进入队列, Nagios每次都从队首开始读取信息,并进行处理后,把状态结果通过web显示出来. N

Nagios插件化之ganglia插件

架构: nagios跟ganglia一样可以都是监控软件,但是两个也有不同之处,Ganglia的优势在于实时对监控集群中的机器的各项指标,比如CPU.内存.磁盘.温度等数据,汇总成各种图形化界面,并提供可调用数据.而在出现问题的时候报警提示功能,相对较弱. Nagios的优势在于出现问题之时可以提供强大的报警提示功能,但是在时间监控上,功能比较弱,即使使用NRPE插件也不足以提供强大的机器监控,所以此处将两者结合起来.实现监控.报警功能. 环境介绍: 1.服务器中已经安装好了Ganglia,安装

自定义nagios插件实现主动被动模式以及nagios基于mail的简单告警

nagios插件程序提供两个返回值:一个是插件的退出状态码,另一个是插件在控制台上打印的第一行数据.退出状态码可以被nagios主程序 作为判断被监控系统服务状态的依据,控制台打印的第一行数据可以被nagios主程序作为被监控系统服务状态的补充说明 会显示在管理页面里面. 为了管理nagios插件,nagios每查询一个服务的状态时,就会产生一个子进程,并且它使用来自该命令的输出和退出状态码来 确定具体的状态.nagios主程序可识别的状态码和说明如下: OK            退出代码 0

合nagios+cacti+微信、飞信实现网络监控报警

系统环境:rhel6.3         selinux disabled  和 iptables     整合cacti和nagios是利用了cacti的一个插件nagiosfor cacti,它的原理是将nagios的数据通过ndo2db导入到mysql数据库(cacti的库中),然后cacti读取数据库信息将nagios的结果展示出来. 一.nagios监控本地主机 注释掉localhost.cfg,新增加hosts.cfg,services.cfg [[email protected]

nagios 实现Mysql 主从同步状态的监控

一.系统环境 主机名 IP nagios 192.168.15.111 mysql_s 192.168.15.21 二.操作步骤 2.1 mysql_s端的配置 2.1.1 编写check_mysql_slave监控脚本 cd /usr/local/nagios/libexec   #切换到nagios 监控插件所在目录 vim check_mysql_slave       #开始编写mysql_slave监控脚本 注意:监控脚本中的mysql账户一定要新建一个,并设置有限的权限.   2.1

开发者福利!ChromeSnifferPlus 插件正式登陆 Chrome Web Store

今天(2014-10-30)下午,ChromeSnifferPlus 插件正式登陆 Chrome Web Store. 在线安装地址: https://chrome.google.com/webstore/detail/chrome-sniffer-plus/fhhdlnnepfjhlhilgmeepgkhjmhhhjkh Chrome 探测器,可以探测正在使用的开源软件或者 js 类库,开发者必备. 通过本插件,您可以探测: javascript 库: jQuery.ExtJS.Angular