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 exitstatus=OK;
char *exit_status[4]={"OK","WARNING","CRITICAL","UNKNOWN"};

char status_information[LEN];
char performance_data[LEN];

int parse_cmd() {
        int ret=0;
        FILE *fp;
        char readbuf[LEN];

        int i;
        char *p,*str;

        float used_percent=0;
        char used_handle[LEN]={0},free_handle[LEN]={0},max_handle[LEN]={0};

        fp=fopen(FILE_NR,"r");
        if(fp==NULL) {
        //      fprintf(stderr,"fopen() error.\n");

                sprintf(status_information,"File not exist, used_handle=%s max_handle=%s used_percent=%.0f%%","0","0",used_percent*100);

                sprintf(performance_data,"used_handle=%s;;;; max_handle=%s;;;; used_percent=%.0f%%;30;50;;","0","0",used_percent*100);

                exitstatus=CRITICAL;

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

        str=fgets(readbuf,LEN,fp);
        if(str==NULL) {
                fprintf(stderr,"fgets() error.\n");
                return -1;
        }
//      printf("readbuf=%s",readbuf);

        ret=sscanf(readbuf,"%s\t%s\t%s",used_handle,free_handle,max_handle);
//      printf("sscanf ret=%d\n",ret);

//      printf("used_handle=%s,free_handle=%s,max_handle=%s\n",used_handle,free_handle,max_handle);

        used_percent=(float)(atoi(used_handle)/(float)atoi(max_handle));

//      printf("used_percent=%f\n",used_percent);
//      printf("used_percent=%.0f\n",used_percent*100);

        if(used_percent>=0 && used_percent<30) {
                exitstatus=OK;
        }
        else if(used_percent>=30 && used_percent<50) {
                exitstatus=WARNING;
        }
        else if(used_percent>=50 && used_percent<=100) {
                exitstatus=CRITICAL;
        }
        else {
                exitstatus=CRITICAL;
        }

        sprintf(status_information,"used_handle=%s max_handle=%s used_percent=%.0f%%",used_handle,max_handle,used_percent*100);
        sprintf(performance_data,"used_handle=%s;;;; max_handle=%s;;;; used_percent=%.0f%%;30;50;;",used_handle,max_handle,used_percent*100);

        ret=fclose(fp);
        if(ret==EOF) {
                fprintf(stderr,"fclose() error.\n");
                return -1;
        }

       // while(fgets(readbuf,1024,fp)!=NULL) {
                /*
                for(p=strtok(readbuf," ");p;p=strtok(NULL," ")) {
                //      str=p;

                        sprintf(status_information,"active call=%s",p);

                        sprintf(performance_data,"call=%s;;;;",p);

                        break;
                }

                break;
                */

        return 0;
}

int main() {
        int ret;

        ret=parse_cmd();
        if(ret!=0) {
                fprintf(stderr,"parse_cmd() error.\n");
               // exitstatus=CRITICAL;
               // printf("%s: - %s | %s\n",exit_status[exitstatus],status_information,performance_data);
        //      exit(-1);
        }

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

        return exitstatus;
}
时间: 2024-08-08 10:17:09

nagios插件之系统打开文件数监控的相关文章

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

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 "

修改Linux系统打开文件数限制

步骤 1     修改/etc/security/limits.conf文件,在文件中添加两行 # vi /etc/security/limits.conf 添加内容: speng soft nofile 10240 speng hard nofile 10240 步骤 2     修改/etc/pam.d/login文件,在文件中添加一行 # vi /etc/pam.d/login 添加内容: session required /lib/security/pam_limits.so

linux系统打开文件数过多的解决办法

一.永久生效.1. /etc/pam.d/login 添加       session required /lib/security/pam_limits.so2. /etc/security/limits.conf 添加       * soft nofile 65536       * hard nofile 655363.vi /etc/sysctl.conf 添加       fs.file-max = 6553600二.临时生效.   ulimit -n 65536   或将该命令写入

Linux 打开文件数1024限制的原理以及解决办法

/proc/sys/fs/file-max  该文件指定了可以分配的文件句柄的最大数目. 查看最大值: [[email protected] home]# cat /proc/sys/fs/file-max  100977 [[email protected] home]# 这表明这台Linux系统最多允许同时打开(即包含所有用户打开文件数总和)100977个文件,是Linux系统级硬限制,所有用户级的打开文件数限制都不应超过这个数值.通常这个系统级硬限制是Linux系统在启动时根据系统硬件资源

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

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

合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插件化之ganglia插件

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

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

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