vi check_mem.c
/*
Ver: 1.0
Date: 2012-11-20
Desc:the check_mem.c only for linux
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OK 0
#define WARNING 1
#define CRITICAL 2
#define UNKNOWN 3
#define VERSION "1.0"
#define MEMINFO "/proc/meminfo"
int debug=0;
int rawOutput=0;
char pctWarning[32],pctCritical[32],pctSwpWarning[32],pctSwpCritical[32];
void print_usage(char str[32]) {
printf("Usage: %s [options]\n",str);
printf(" e.g. %s -w 75 -W 25 -c 95 -C 75\n\n",str);
printf("Options:\n");
printf("\t --help | -h print help\n");
printf("\t --version | -V print version\n");
printf("\t --verbose | -v be verbose (debug mode)\n");
printf("\t --raw | -r Use MB instead of % for output data\n");
printf("\t -w [int] set warning value for physical RAM used %\n");
printf("\t -c [int] set critical value for physical RAM used %\n");
printf("\t -W [int] set warning value for swap used %\n");
printf("\t -C [int] set critical value for swap used %\n");
printf("\n\n");
}
void print_help(char str[32]) {
printf("check_mem Revision: 1.0\n\n");
printf("This plugin checks file /proc/meminfo\n\n");
print_usage(str);
}
int parse_options(int argc,char *argv[]) {
int j;
for(j=1;j<=argc-1;j++) {
if(strcmp("--help",argv[j])==0 || strcmp("-h",argv[j])==0) {
print_help(argv[j]);
exit(UNKNOWN);
}
else if(strcmp("--version",argv[j])==0 || strcmp("-V",argv[j])==0) {
printf("%s Reversion: %s\n",argv[0],VERSION);
exit(UNKNOWN);
}
else if(strcmp("--verbose",argv[j])==0 || strcmp("-v",argv[j])==0) {
debug=1;
}
else if(strcmp("--rawOutput",argv[j])==0 || strcmp("-r",argv[j])==0) {
rawOutput=1;
}
else if(strcmp("-w",argv[j])==0) {
strcpy(pctWarning,argv[++j]);
}
else if(strcmp("-W",argv[j])==0) {
strcpy(pctSwpWarning,argv[++j]);
}
else if(strcmp("-c",argv[j])==0) {
strcpy(pctCritical,argv[++j]);
}
else if(strcmp("-C",argv[j])==0) {
strcpy(pctSwpCritical,argv[++j]);
}
// else if(argc==2) {
else {
printf("Unknown option!\n");
print_usage(argv[0]);
exit(UNKNOWN);
}
}
return 0;
}
int main(int argc,char *argv[]) {
int a;
int ret;
int i=0,mark=0;
FILE *fp;
char readbuf[64];
int MemTotal,MemFree,Buffers,Cached,MemUse,AllMemUse;;
int SwapTotal,SwapFree,SwapUse;
FILE *cmd;
char str_cmd[512];
// int j;
memset(pctWarning,0,sizeof(pctWarning));
memset(pctCritical,0,sizeof(pctCritical));
memset(pctSwpWarning,0,sizeof(pctSwpWarning));
memset(pctSwpCritical,0,sizeof(pctSwpCritical));
float MemUsePercent;
float SwapUsePercent;
float temp1;
char swap_temp[32],swap_temp_1[32];
int exitstatus=OK;
char *exit_status[4]={"OK","WARNING","CRITICAL","UNKNOWN"};
char result[512];
char perfdata[1024];
printf("argc=%d,argv[1]=%s,argv[2]=%s\n",argc,argv[1],argv[2]);
if(argc>1) {
ret=parse_options(argc,argv);
if(ret!=0) {
fprintf(stderr,"parse_options() error.\n");
exit(-1);
}
}
/*
for(j=1;j<=argc-1;j++) {
switch(argv[j]) {
// case "-h"|"--help":
case "-h":
print_help(argv[1]);
break;
default:
printf("kkkkkkkkkkkkkkkkkkk\n");
break;
}
}
*/
fp=fopen(MEMINFO,"r");
if(fp==NULL) {
fprintf(stderr,"fopen() error.\n");
exit(-1);
}
while(fgets(readbuf,64,fp)!=NULL) {
mark=0;
if(strstr(readbuf,"MemTotal")) {
ret=sscanf(readbuf,"MemTotal: %d kB",&MemTotal);
mark=1;
}
else if(strstr(readbuf,"MemFree:")) {
ret=sscanf(readbuf,"MemFree: %d kB",&MemFree);
mark=1;
}
else if(strstr(readbuf,"Buffers:")) {
ret=sscanf(readbuf,"Buffers: %d kB",&Buffers);
mark=1;
}
else if(strstr(readbuf,"Cached:")) {
ret=sscanf(readbuf,"Cached: %d kB",&Cached);
mark=1;
}
else if(strstr(readbuf,"SwapTotal:")) {
ret=sscanf(readbuf,"SwapTotal: %d kB",&SwapTotal);
mark=1;
}
else if(strstr(readbuf,"SwapFree:")) {
ret=sscanf(readbuf,"SwapFree: %d kB",&SwapFree);
mark=1;
}
if(mark==1) {
i++;
}
if(i==7) {
break;
}
}
ret=fclose(fp);
if(ret==EOF) {
fprintf(stderr,"fclose() error.\n");
exit(-1);
}
MemUse=MemTotal-MemFree-Buffers-Cached;
// printf("MemUse=%dKB\n",MemUse);
AllMemUse=MemTotal-MemFree;
// printf("AllMemUse=%dKB\n",AllMemUse);
MemUsePercent=(float)MemUse/(float)MemTotal*100;
// printf("PctMemUse=%.2f%\n",MemUsePercent);
SwapUse=SwapTotal-SwapFree;
// printf("SwapUse=%dKB\n",SwapUse);
SwapUsePercent=(float)SwapUse/(float)SwapTotal*100;
// printf("PctSwapUse=%.2f%\n",SwapUsePercent);
// printf("%.3f%\n",(float)MemUse/(float)MemTotal*100);
// printf("MemUse=%.2f%\n",(float)MemUse/(float)MemTotal*100);
if(debug==1) {
cmd=popen("free -mt","r");
if(cmd==NULL) {
fprintf(stderr,"popen() error.\n");
}
else {
while(fgets(str_cmd,512,cmd)!=NULL)
printf("%s",str_cmd);
}
// printf("---debug---\n");
printf("phyTotal=%d\n",MemTotal/1024);
printf("swpTotal=%d\n",SwapTotal/1024);
printf("phyShared=%d\n",0);
printf("phyBuffers=%d\n",Buffers/1024);
printf("phyCached=%d\n",Cached/1024);
printf("phyUsed=%d\n",MemUse/1024);
printf("phyAllUsed=%d\n",AllMemUse/1024);
printf("swpUsed=%d\n",SwapUse/1024);
printf("pctPhyShared=%d\n",0);
printf("pctPhyBuffers=%.2f\n",(float)Buffers/(float)MemTotal*100);
printf("pctPhyCached=%.2f",(float)Cached/(float)MemTotal*100);
printf("pctPhyUsed=%.2f\n",(float)MemUse/(float)MemTotal*100);
printf("pctPhyAllUsed=%.2f\n",(float)AllMemUse/(float)MemTotal*100);
snprintf(swap_temp_1,32,"%.2f",SwapUsePercent);
if(!strcmp(swap_temp_1,"0.00"))
printf("pctSwpUsed=%.f\n",SwapUsePercent);
else
printf("pctSwpUsed=%.2f\n",SwapUsePercent);
printf("rawOutput=%d\n",0);
printf("pctWarning=%d\n",0);
printf("pctCritical=%d\n",0);
printf("pctSwpWarning=%d\n",0);
printf("pctSwpCritical=%d\n",0);
// printf("-----------\n");
}
if(argc==1) {
if((int)MemUsePercent>=80 && (int)MemUsePercent<=90)
exitstatus=WARNING;
else if((int)MemUsePercent>90)
exitstatus=CRITICAL;
if((int)SwapUsePercent>=80 && (int)SwapUsePercent<=90)
exitstatus=WARNING;
else if((int)SwapUsePercent>90)
exitstatus=CRITICAL;
}
else {
if(strlen(pctWarning))
if((int)MemUsePercent>=atoi(pctWarning))
exitstatus=WARNING;
if(strlen(pctSwpWarning))
if((int)SwapUsePercent>=atoi(pctSwpWarning))
exitstatus=WARNING;
if(strlen(pctCritical))
if((int)MemUsePercent>=atoi(pctCritical))
exitstatus=CRITICAL;
if(strlen(pctSwpCritical))
if((int)SwapUsePercent>=atoi(pctSwpCritical))
exitstatus=CRITICAL;
}
printf("exitstatus=%d\n",exitstatus);
snprintf(swap_temp,32,"%.2f",SwapUsePercent);
if(!strcmp(swap_temp,"0.00")) {
if(rawOutput==1) {
snprintf(result,512,"Memory Usage - %dMB of %dMB RAM used, %dMB of %dMB Swap used",MemUse/1024,MemTotal/1024,SwapUse/1024,SwapTotal/1024);
snprintf(perfdata,1024,"phyUsed=%d;%s;%s;0;%d phyShared=0;;;0;%d phyBuffers=%d;;;0;%d phyCached=%d;;;0;%d phyAllUsed=%d;;;0;%d swpUsed=%d;%s;%s;0;%d",MemUse/1024,pctWarning,pctCritical,MemTotal/1024,MemTotal/1024,Buffers/1024,MemTotal/1024,Cached/1024,MemTotal/1024,AllMemUse/1024,MemTotal/1024,SwapUse/1024,pctSwpWarning,pctSwpCritical,SwapTotal/1024);
}
else {
snprintf(result,512,"Memory Usage - %.2f%s RAM, %.f%s Swap",MemUsePercent,"%",SwapUsePercent,"%");
snprintf(perfdata,1024,"phyUsed=%.2f%;%s;%s;0;100 phyShared=0%;;;0;100 phyBuffers=%.2f%;;;0;100 phyCached=%.2f%s;;;0;100 phyAllUsed=%.2f%s;;;0;100 swpUsed=%.f%;%s;%s;0;100",MemUsePercent,pctWarning,pctCritical,(float)Buffers/(float)MemTotal*100,(float)Cached/(float)MemTotal*100,"%",(float)AllMemUse/(float)MemTotal*100,"%",SwapUsePercent,pctSwpWarning,pctSwpCritical);
}
}
else {
if(rawOutput==1) {
snprintf(result,512,"Memory Usage - %dMB of %dMB RAM used, %dMB of %dMB Swap used",MemUse/1024,MemTotal/1024,SwapUse/1024,SwapTotal/1024);
snprintf(perfdata,1024,"phyUsed=%d;%s;%s;0;%d phyShared=0;;;0;%d phyBuffers=%d;;;0;%d phyCached=%d;;;0;%d phyAllUsed=%d;;;0;%d swpUsed=%d;;;0;%d",MemUse/1024,pctWarning,pctCritical,MemTotal/1024,MemTotal/1024,Buffers/1024,MemTotal/1024,Cached/1024,MemTotal/1024,AllMemUse/1024,MemTotal/1024,SwapUse/1024,SwapTotal/1024);
}
else {
snprintf(result,512,"Memory Usage - %.2f%s RAM, %.2f%s Swap",MemUsePercent,"%",SwapUsePercent,"%");
snprintf(perfdata,1024,"phyUsed=%.2f%;%s;%s;0;100 phyShared=0%;;;0;100 phyBuffers=%.2f%;;;0;100 phyCached=%.2f%s;;;0;100 phyAllUsed=%.2f%s;;;0;100 swpUsed=%.2f%s;%s;%s;0;100",MemUsePercent,pctWarning,pctCritical,(float)Buffers/(float)MemTotal*100,(float)Cached/(float)MemTotal*100,"%",(float)AllMemUse/(float)MemTotal*100,"%",SwapUsePercent,"%");
}
}
printf("%s: %s",exit_status[exitstatus],result);
printf(" | ");
printf("%s\n",perfdata);
return exitstatus;
}