设为首页 收藏本站
查看: 814|回复: 0

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

[复制链接]

尚未签到

发表于 2015-11-23 08:23:13 | 显示全部楼层 |阅读模式
  执行: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 UNKNOWN  3
#define LEN 1000
#define MIN_LEN 20
#define SHORT_TIME 1// unit of minute
#define LONG_TIME 10// unit of minute
//#define TCL_CMD &quot;/home/weihu/tcl/&quot;
#define TCL_CMD &quot;/usr/local/nagios/libexec/&quot;
#define SBC_GG_SHORT_TIME_FILE &quot;/usr/local/nagios/libexec/sbc_gg_call_short_time_file.tmp&quot;
#define SBC_YZ_SHORT_TIME_FILE &quot;/usr/local/nagios/libexec/sbc_yz_call_short_time_file.tmp&quot;
//#define SBC_SHORT_TIME_FILE &quot;/home/tssp/sbc/sbc_call_short_time_file.tmp&quot;
#define SBC_GG_LONG_TIME_FILE &quot;/usr/local/nagios/libexec/sbc_gg_call_long_time_file.tmp&quot;
#define SBC_YZ_LONG_TIME_FILE &quot;/usr/local/nagios/libexec/sbc_yz_call_long_time_file.tmp&quot;
//#define SBC_LONG_TIME_FILE &quot;/home/tssp/sbc/sbc_call_long_time_file.tmp&quot;
int exitstatus=OK;
char *exit_status[4]={&quot;OK&quot;,&quot;WARNING&quot;,&quot;CRITICAL&quot;,&quot;UNKNOWN&quot;};
char status_information[LEN];
char performance_data[LEN];
//char sbc_gg_old_value[MIN_LEN]={0};
//char sbc_yz_old_value[MIN_LEN]={0};
int read_time_file(int minute,char *file) {
int ret;
FILE *fp;
char *ch;
char readbuf[20]={0};
int now_minute=0;
//fp=fopen(file,&quot;a+&quot;);
fp=fopen(file,&quot;r+&quot;);
if(fp==NULL) {
fprintf(stderr,&quot;fopen() error.\n&quot;);
return -1;
}
ch=fgets(readbuf,20,fp);
if(ch==NULL) {
fprintf(stderr,&quot;fgets() error.\n&quot;);
//return -1;
}
printf(&quot;readbuf=%s\n&quot;,readbuf);
now_minute=atoi(readbuf);
printf(&quot;now_minute=%d\n&quot;,now_minute);
if(!strcmp(readbuf,&quot;&quot;)) {
if(minute==SHORT_TIME) {
if(now_minute<SHORT_TIME-1) {
exitstatus=WARNING;
}
else if(now_minute>=SHORT_TIME-1) {
exitstatus=CRITICAL;
}
}
else if(minute==LONG_TIME) {
/*if(now_minute<LONG_TIME-1) {
exitstatus=WARNING;
}
else if(now_minute>=LONG_TIME-1) {
*/
if(now_minute>=LONG_TIME-1) {
exitstatus=CRITICAL;
}
}
fprintf(fp,&quot;%s&quot;,&quot;1&quot;);
printf(&quot;file is null,value=1\n&quot;);
}
else {
if(minute==SHORT_TIME) {
if(now_minute<SHORT_TIME-1) {
exitstatus=WARNING;
ret=fseek(fp,0,SEEK_SET);
fprintf(fp,&quot;%d&quot;,++now_minute);
/*
fclose(fp);
fp=fopen(file,&quot;w+&quot;);
fprintf(fp,&quot;%d&quot;,now_minute++);
*/
}
else if(now_minute>=SHORT_TIME-1) {
exitstatus=CRITICAL;
ret=fseek(fp,0,SEEK_SET);
fprintf(fp,&quot;%d&quot;,++now_minute);
}
}
else if(minute==LONG_TIME) {
if(now_minute<LONG_TIME-1) {
//exitstatus=WARNING;
exitstatus=OK;
ret=fseek(fp,0,SEEK_SET);
fprintf(fp,&quot;%d&quot;,++now_minute);
}
else if(now_minute>=LONG_TIME-1) {
//if(now_minute>=LONG_TIME-1) {
exitstatus=CRITICAL;
ret=fseek(fp,0,SEEK_SET);
fprintf(fp,&quot;%d&quot;,++now_minute);
}
}
}
ret=fclose(fp);
if(ret!=0) {
fprintf(stderr,&quot;fclose() error.\n&quot;);
return -1;
}
return 0;
}
int parse_cmd(char *sh_cmd,char *active_count,char *high_count,char *total_count) {
int ret;
FILE *fp;
char tmpbuf[LEN];
char readbuf[LEN];
char *p,*str;
int i=0;
int line=0;
int mark1=0;
int mark2=0;
//fp=popen(&quot;/home/neo/check_log/tcl/auto_ssh.sh&quot;,&quot;r&quot;);
fp=popen(sh_cmd,&quot;r&quot;);
if(fp==NULL) {
fprintf(stderr,&quot;popen() error. &quot;);
exitstatus=CRITICAL;
printf(&quot;%s: - %s | %s\n&quot;,exit_status[exitstatus],status_information,performance_data);
exit(exitstatus);
}
while(fgets(readbuf,LEN,fp)!=NULL) {
//line++;
if(strstr(readbuf,&quot;SIP Sessions&quot;)) {
memset(tmpbuf,0,LEN);
strcpy(tmpbuf,readbuf);
//printf(&quot;%s&quot;,tmpbuf);
for(p=strtok(tmpbuf,&quot; &quot;);p;p=strtok(NULL,&quot; &quot;),i++) {
if(i==2) {
//printf(&quot;%s\n&quot;,p);
strcpy(active_count,p);
}
if(i==3) {
//printf(&quot;%s\n&quot;,p);
strcpy(high_count,p);
}
if(i==4) {
//printf(&quot;%s\n&quot;,p);
strcpy(total_count,p);
break;
}
}
}
if(i==4) {
break;
}
/*
line++;
//printf(&quot;line=%d,readbuf=%s&quot;,line,readbuf);
if(line==3) {
for(p=strtok(readbuf,&quot; &quot;);p;p=strtok(NULL,&quot; &quot;)) {
//      str=p;
//Sun
mark1++;
if(mark1==2) {
//printf(&quot;p=%s\n&quot;,p);
strcpy(active_status,p);
//printf(&quot;active_status=%s\n&quot;,active_status);
}
}
}
if(line==4) {
for(p=strtok(readbuf,&quot; &quot;);p;p=strtok(NULL,&quot; /&quot;)) {
mark2++;
if(mark2==2) {
//printf(&quot;p=%s\n&quot;,p);
strcpy(active_ip_addr,p);
//printf(&quot;active_ip_addr=%s\n&quot;,active_ip_addr);
}
}
break;
}
*/
}
ret=fclose(fp);
if(fp==NULL) {
fprintf(stderr,&quot;popen() error.\n&quot;);
return -1;
}
return 0;
}
int main(int argc, char *argv[]) {
int ret;
int hour;
char str_hour[LEN];
char sh_cmd[LEN];
char active_count[LEN];
char high_count[LEN];
char total_count[LEN];
char active_ip_addr[LEN];
time_t timestamp;
struct tm *tp;
FILE *fp;
if(argc<=1) {
printf(&quot;%s %s\n&quot;,argv[0],&quot;auto_ssh_sbc_10_17.sh | auto_ssh_sbc_11_17.sh&quot;);
exit(-1);
}
timestamp=time(NULL);
tp=localtime(&#215;tamp);
sprintf(str_hour,&quot;%d&quot;,tp->tm_hour);
//sprintf(str_hour,&quot;%d&quot;,3);
hour=atoi(str_hour);
//printf(&quot;hour=%d\n&quot;,hour);
sprintf(sh_cmd,&quot;%s%s&quot;,TCL_CMD,argv[1]);
//printf(&quot;sh_cmd=%s\n&quot;,sh_cmd);
ret=parse_cmd(sh_cmd,active_count,high_count,total_count);
if(ret!=0) {
fprintf(stderr,&quot;parse_cmd() error.\n&quot;);
exit(-1);
}
//strcpy(active_count,&quot;0&quot;);
//strcpy(active_count,&quot;1&quot;);
/*
printf(&quot;active_count=%s\n&quot;,active_count);
printf(&quot;high_count=%s\n&quot;,high_count);
printf(&quot;total_count=%s\n&quot;,total_count);
*/
if(!strcmp(active_count,&quot;0&quot;)) {
if(hour>=0 && hour<=6 || hour==23) {
if(!strcmp(&quot;auto_ssh_sbc_10_17.sh&quot;,argv[1])){
ret=read_time_file(LONG_TIME,SBC_GG_LONG_TIME_FILE);
if(ret!=0) {
fprintf(stderr,&quot;read_time_file(SBC_GG_LONG_TIME_FILE) error.\n&quot;);
}
}
else if(!strcmp(&quot;auto_ssh_sbc_11_17.sh&quot;,argv[1])){
ret=read_time_file(LONG_TIME,SBC_YZ_LONG_TIME_FILE);
if(ret!=0) {
fprintf(stderr,&quot;read_time_file(SBC_YZ_LONG_TIME_FILE) error.\n&quot;);
}
}
}
else if(hour>6 && hour<=22){
if(!strcmp(&quot;auto_ssh_sbc_10_17.sh&quot;,argv[1])){
ret=read_time_file(SHORT_TIME,SBC_GG_SHORT_TIME_FILE);
if(ret!=0) {
fprintf(stderr,&quot;read_time_file(SBC_GG_SHORT_TIME_FILE) error.\n&quot;);
}
}
else if(!strcmp(&quot;auto_ssh_sbc_11_17.sh&quot;,argv[1])){
ret=read_time_file(SHORT_TIME,SBC_YZ_SHORT_TIME_FILE);
if(ret!=0) {
fprintf(stderr,&quot;read_time_file(SBC_YZ_SHORT_TIME_FILE) error.\n&quot;);
}
}
}
else {
exitstatus=UNKNOWN;
}
}
else {
if(!strcmp(&quot;auto_ssh_sbc_10_17.sh&quot;,argv[1])){
fp=fopen(SBC_GG_SHORT_TIME_FILE,&quot;w+&quot;);
fprintf(fp,&quot;%d&quot;,0);
fp=fopen(SBC_GG_LONG_TIME_FILE,&quot;w+&quot;);
fprintf(fp,&quot;%d&quot;,0);
}
else if(!strcmp(&quot;auto_ssh_sbc_11_17.sh&quot;,argv[1])){
fp=fopen(SBC_YZ_SHORT_TIME_FILE,&quot;w+&quot;);
fprintf(fp,&quot;%d&quot;,0);
fp=fopen(SBC_YZ_LONG_TIME_FILE,&quot;w+&quot;);
fprintf(fp,&quot;%d&quot;,0);
}
ret=fclose(fp);
}
//exitstatus=OK;
sprintf(status_information,&quot;active_count=%s, high_count=%s, total_count=%s&quot;,active_count,high_count,total_count);
sprintf(performance_data,&quot;active_count=%s;;;; high_count=%s;;;; total_count=%s;;;;&quot;,active_count,high_count,total_count);
/*
if(strstr(argv[1],&quot;148&quot;)) {
if(!strcmp(active_ip_addr,&quot;114.66.80.148&quot;) && !strcmp(active_status,&quot;active&quot;)) {
exitstatus=OK;
sprintf(status_information,&quot;f5 Active=%s, Standby=%s&quot;,&quot;114.66.80.148&quot;,&quot;114.66.80.149&quot;);
sprintf(performance_data,&quot;Master=%d;;;; Slave=%d;;;;&quot;,1,0);
}
//else if(!strcmp(active_ip_addr,&quot;114.66.80.149&quot;) && !strcmp(active_status,&quot;active&quot;)) {
else if(!strcmp(active_ip_addr,&quot;114.66.80.148&quot;) && !strcmp(active_status,&quot;standby&quot;)) {
exitstatus=WARNING;
sprintf(status_information,&quot;f5 Active=%s, Standby=%s&quot;,&quot;114.66.80.149&quot;,&quot;114.66.80.148&quot;);
sprintf(performance_data,&quot;Master=%d;;;; Slave=%d;;;;&quot;,0,1);
}
}
else if(strstr(argv[1],&quot;149&quot;)) {
if(!strcmp(active_ip_addr,&quot;114.66.80.149&quot;) && !strcmp(active_status,&quot;standby&quot;)) {
exitstatus=OK;
sprintf(status_information,&quot;f5 Active=%s, Standby=%s&quot;,&quot;114.66.80.148&quot;,&quot;114.66.80.149&quot;);
sprintf(performance_data,&quot;Master=%d;;;; Slave=%d;;;;&quot;,1,0);
}
//else if(!strcmp(active_ip_addr,&quot;114.66.80.148&quot;) && !strcmp(active_status,&quot;active&quot;)) {
else if(!strcmp(active_ip_addr,&quot;114.66.80.149&quot;) && !strcmp(active_status,&quot;active&quot;)) {
exitstatus=WARNING;
sprintf(status_information,&quot;f5 Active=%s, Standby=%s&quot;,&quot;114.66.80.149&quot;,&quot;114.66.80.148&quot;);
sprintf(performance_data,&quot;Master=%d;;;; Slave=%d;;;;&quot;,0,1);
}
}
*/
printf(&quot;%s: %s | %s\n&quot;,exit_status[exitstatus],status_information,performance_data);
return exitstatus;
}
  

vi auto_ssh_sbc_10_17.sh

#!/usr/bin/expect -f
#set port 22
set user user
set host 10.127.10.17
#set host 114.66.80.149
set password password
#set timeout -1
set timeout 10
#spawn ssh -D $port $user@$host
#spawn ssh $user@$host date
#spawn ssh $user@$host b failover show \; ip ad | grep 'inet 114.66.80.14'
##spawn ssh $user@$host show sessions
#spawn ssh $user@$host
spawn telnet $host
expect &quot;*assword:*&quot;
send &quot;$password\r&quot;
expect '*>*'
send &quot;show sessions\r&quot;
expect eof
  

vi auto_ssh_sbc_11_17.sh

#!/usr/bin/expect -f
#set port 22
set user user
set host 10.127.11.17
#set host 114.66.80.149
set password password
#set timeout -1
set timeout 10
#spawn ssh -D $port $user@$host
#spawn ssh $user@$host date
#spawn ssh $user@$host b failover show \; ip ad | grep 'inet 114.66.80.14'
##spawn ssh $user@$host show sessions
##spawn ssh $user@$host
spawn telnet $host
expect &quot;*assword:*&quot;
send &quot;$password\r&quot;
expect '*>*'
send &quot;show sessions\r&quot;
expect eof
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-142350-1-1.html 上篇帖子: 用python 写一个nagios插件 监控http内容 下篇帖子: nagios安装配置笔记
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表