nagios插件之监控文件更新时间

vi check_file_updae_time.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <ifaddrs.h>
#include <netinet/in.h> 
#include <arpa/inet.h>
#include <string.h> 

#define OK       0
#define WARNING  1
#define CRITICAL 2
#define UNKNOWN  3

#define LEN 1023
//#define LOG_FILE "/var/log/asterisk/cdr-csv/Master.csv"
////#define LOG_FILE "/DATE/anylink/probus5/logs/SPZHJJR1/SPZHJJR1_RTEcontainer1/servlet/accesslog/access.log"
#define LOG_FILE "/DATE/anylink/probus5/logs/SPZHJJR4/SPZHJJR4_RTEcontainer1/servlet/accesslog/access.log"
//#define LOG_FILE "/home/uniadmin/neo/test/check_file_time/Master.csv"

#define SHORT_TIME 300
#define LONG_TIME 1200
//#define TIME 60


int exitstatus=OK;
char *exit_status[4]={"OK","WARNING","CRITICAL","UNKNOWN"};

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

time_t timestamp;


int is_master_or_slave(void) {
        int mark=0;
        struct ifaddrs * ifAddrStruct=NULL;
        void * tmpAddrPtr=NULL;
        char addressBuffer[INET_ADDRSTRLEN];

        getifaddrs(&ifAddrStruct);

        while(ifAddrStruct!=NULL) {
                tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;

                inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);

        //      if (ifAddrStruct->ifa_addr->sa_family==AF_INET) { 
        //      if (!strncmp(addressBuffer,"10.199.75.169",13)) { 

                if (!strcmp(addressBuffer,"10.127.2.137")) { 
                //      printf("%s IP Address %s\n", ifAddrStruct->ifa_name, addressBuffer); 
                        mark=1;
                        break;
                }

                ifAddrStruct=ifAddrStruct->ifa_next;
        }

        if(mark==1) {
                return 1;
        }
        else
                return 0;
}

int diff_time(int istime) {
        int ret;
        int result=0;
        struct stat getstat;


//      timestamp=time(NULL);
//      printf("now time=%d\n",timestamp);

        ret=stat(LOG_FILE,&getstat);
        if(ret==-1) {
                if(errno==ENOENT) {
                        fprintf(stderr,"File or Patch not found.\n");

                        sprintf(status_information,"File or Patch not found.");
                        exitstatus=CRITICAL;

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

                        return exitstatus;
                }
                else {
                        fprintf(stderr,"Other error.\n");

                        sprintf(status_information,"Other error.");
                        exitstatus=CRITICAL;

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

                        return exitstatus;
                }
        }
//      printf("file time=%d\n",getstat.st_mtime);

        result=timestamp-getstat.st_mtime;
//      printf("result=%d\n",result);

//      ret=is_master_or_slave();

        ret=1;

        if(result>=istime) { 
                if(ret==1) {
                        sprintf(status_information,"is Master, access.log Status=%d Second=%d",0,result);
                        sprintf(performance_data,"Status=%d;;;; Second=%d;;;;",0,result);
                        exitstatus=CRITICAL;
                }
                else {
                        sprintf(status_information,"is Slave, access.log Status=%d Second=%d",0,result);
                        sprintf(performance_data,"Status=%d;;;; Second=%d;;;;",0,result);
                        exitstatus=OK;
                }
        }
        else {
                if(ret==1) {
                        sprintf(status_information,"is Master, access.log Status=%d Second=%d",1,result);
                        sprintf(performance_data,"Status=%d;;;; Second=%d;;;;",1,result);
                        exitstatus=OK;
                }
                else {
                        sprintf(status_information,"is Slave, access.log Status=%d Second=%d",1,result);
                        sprintf(performance_data,"Status=%d;;;; Second=%d;;;;",1,result);
                        exitstatus=OK;
                }
        }

        return 0;
}

int main() {
        int ret;

        int hour;
        char hour_str[LEN];

        struct tm *tp;

        timestamp=time(NULL);

        tp=localtime(×tamp);
        sprintf(hour_str,"%d",tp->tm_hour);
        hour=atoi(hour_str);

//      printf("hour=%d\n",hour);

        if(hour>=1 || hour<=7) {
                ret=diff_time(LONG_TIME);
                if(ret!=0) {
                        fprintf(stderr,"diff_time(SHORT_TIME) error.\n");
                }
        }
        else {
                ret=diff_time(SHORT_TIME);
                if(ret!=0) {
                        fprintf(stderr,"diff_time(LONG_TIME) error.\n");
                }
        }

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

        return exitstatus;
}


你可能感兴趣的:(nagios插件之监控文件更新时间)