vi check_vp_call.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#define OK 0
#define WARNING 1
#define CRITICAL 2
#define UNKNOWN 3
#define LEN 1023
#define LOG_FILE "/home/craft/check_log/check_vp/temp.txt"
int main(void) {
FILE *fp;
int ret;
int fd;
char *p,*str;
char readbuf[LEN];
int m,n;
int max=0;
int call_max=0;
int session_max=0;
int i=0,j=0,k=0;
char *temp;
char *date[LEN];
char *session[LEN];
char *call[LEN];
struct tm *tm_p;
time_t timep;
char year[LEN];
char new_filename[LEN];
char call_filename[LEN];
int exitstatus=OK;
char *exit_status[4]={"OK","WARNING","CRITICAL","UNKNOWN"};
char status_information[LEN];
char performance_data[LEN];
fp=fopen(LOG_FILE,"r+");
if(fp==NULL) {
fprintf(stderr,"fopen() error. ");
exitstatus=CRITICAL;
printf("%s: - %s | %s\n",exit_status[exitstatus],status_information,performance_data);
return exitstatus;
}
fd=fileno(fp);
ret=lockf(fd,F_LOCK,0);
if(ret==-1) {
perror("lockf() F_LOCK");
exit(-1);
}
else {
timep=time(NULL);
tm_p=localtime(&timep);
// printf("%s\n",new_filename);
//
// printf("%s\n",call_filename);
sprintf(year,"%d",1900+tm_p->tm_year);
// printf("year=%s\n",year);
while(fgets(readbuf,LEN,fp)!=NULL) {
// if(strstr(readbuf,"2013")) {
if(strstr(readbuf,year)) {
// for(p=strtok(readbuf," \n");p;p=strtok(NULL," \n"))
// str=p;
temp=(char *)malloc(128);
// m=strlen(readbuf);
readbuf[strlen(readbuf)-1]='\0';
strcpy(temp,readbuf);
date[i++]=temp;
}
else if(strstr(readbuf,"session")) {
for(p=strtok(readbuf," \n");p;p=strtok(NULL," \n"))
str=p;
temp=(char *)malloc(128);
strcpy(temp,str);
session[j++]=temp;
}
else if(strstr(readbuf,"call")) {
for(p=strtok(readbuf," \n");p;p=strtok(NULL," \n"))
str=p;
temp=(char *)malloc(128);
strcpy(temp,str);
call[k++]=temp;
}
}
}
// unlink file
ret=unlink(LOG_FILE);
if(ret==-1) {
perror("unlink()");
}
// unlock
ret=lockf(fd,F_ULOCK,0);
if(ret==-1) {
perror("lockf() F_ULOCK");
exit(-1);
}
ret=fclose(fp);
if(ret==EOF) {
fprintf(stderr,"fclose() error.\n");
exit(-1);
}
// printf("i=%d\n",i);
// for(n=0;n<i;n++)
// printf("%s,%s,%s\n",date[n],session[n],call[n]);
//----------------------------------------
// printf("max=%d\n",max);
for(n=0;n<i;n++) {
if(atoi(call[n])>=max) {
max=atoi(call[n]);
m=i;
}
}
// printf("m=%d\n",m);
// printf("max=%d\n",max);
for(n=0;n<i;n++) {
if(atoi(call[n])==max) {
// printf("%s,session=%s,call=%s\n",date[n],session[n],call[n]);
if(atoi(session[n])>=session_max) {
call_max=n;
session_max=atoi(session[n]);
}
}
}
sprintf(status_information,"current call=%s, session=%s",call[call_max],session[call_max]);
// printf("status_information=%s\n",status_information);
sprintf(performance_data,"call=%s;;;; session=%s;;;;",call[call_max],session[call_max]);
// printf("performance_data=%s\n",performance_data);
printf("%s: %s | %s\n",exit_status[exitstatus],status_information,performance_data);
return exitstatus;
}
本文介绍了一个通过解析日志文件来监控系统中电话呼叫活动的C语言程序。该程序读取特定的日志文件,从中提取日期、会话和呼叫信息,并找出当前的最大呼叫数及其对应的会话数。此外,还实现了文件锁定、解锁和删除等功能。

396

被折叠的 条评论
为什么被折叠?



