VI check_open_file.c (used by Linux systems only)
#include <stdio.h> #include <stdlib.h> #include <string.h> #define OK 0#define WARNING 1#define crit ICAL 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 R et=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);//P rintf ("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 (NUL L, "")) {//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;}
Nagios plug-in system open file count monitoring