check_nt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /******************************************************************************
  2. *
  3. * This file is part of the Nagios Plugins.
  4. *
  5. * Copyright (c) 2000 Yves Rubin <rubiyz@yahoo.com>
  6. *
  7. * The Nagios Plugins are free software; you can redistribute them
  8. * and/or modify them under the terms of the GNU General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. * $Id$
  22. *
  23. *****************************************************************************/
  24. #define PROGRAM check_nt
  25. #define DESCRIPTION "Windows NT plugin for Nagios"
  26. #define AUTHOR "Yves Rubin"
  27. #define EMAIL "rubiyz@yahoo.com"
  28. #define COPYRIGHTDATE "2000"
  29. //#include "stdlib.h"
  30. #include "config.h"
  31. #include "common.h"
  32. #include "netutils.h"
  33. #include "utils.h"
  34. #define CHECK_NONE 0
  35. #define CHECK_CLIENTVERSION 1
  36. #define CHECK_CPULOAD 2
  37. #define CHECK_UPTIME 3
  38. #define CHECK_USEDDISKSPACE 4
  39. #define CHECK_SERVICESTATE 5
  40. #define CHECK_PROCSTATE 6
  41. #define CHECK_MEMUSE 7
  42. #define CHECK_COUNTER 8
  43. #define MAX_VALUE_LIST 30
  44. #define PORT 1248
  45. char *server_address=NULL;
  46. char *volume_name=NULL;
  47. int server_port=PORT;
  48. char *value_list=NULL;
  49. char *req_password=NULL;
  50. unsigned long lvalue_list[MAX_VALUE_LIST];
  51. unsigned long warning_value=0L;
  52. unsigned long critical_value=0L;
  53. int check_value_list=FALSE;
  54. int check_warning_value=FALSE;
  55. int check_critical_value=FALSE;
  56. int vars_to_check=CHECK_NONE;
  57. int show_all=FALSE;
  58. #define PROGNAME "check_nt"
  59. int process_arguments(int, char **);
  60. void print_usage(void);
  61. void print_help(void);
  62. void preparelist(char *string);
  63. int main(int argc, char **argv){
  64. int result;
  65. int return_code;
  66. char *send_buffer=NULL;
  67. char recv_buffer[MAX_INPUT_BUFFER];
  68. char *output_message=NULL;
  69. char *temp_buffer=NULL;
  70. char *temp_string=NULL;
  71. char *sep_string=NULL;
  72. char *description=NULL;
  73. double total_disk_space=0;
  74. double free_disk_space=0;
  75. double percent_used_space=0;
  76. double mem_commitLimit=0;
  77. double mem_commitByte=0;
  78. unsigned long current_connections=0;
  79. unsigned long utilization;
  80. unsigned long uptime;
  81. unsigned long cache_hits;
  82. unsigned long cache_buffers;
  83. unsigned long lru_time;
  84. double counter_value;
  85. int offset=0;
  86. int updays=0;
  87. int uphours=0;
  88. int upminutes=0;
  89. req_password=strscpy(req_password,"None");
  90. if(process_arguments(argc,argv)==ERROR)
  91. usage("Could not parse arguments\n");
  92. /* initialize alarm signal handling */
  93. signal(SIGALRM,socket_timeout_alarm_handler);
  94. /* set socket timeout */
  95. alarm(socket_timeout);
  96. if (vars_to_check==CHECK_CLIENTVERSION) {
  97. send_buffer = strscpy(send_buffer,strcat(req_password,"&1"));
  98. result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
  99. if(result!=STATE_OK)
  100. return result;
  101. output_message = strscpy(output_message,recv_buffer);
  102. return_code=STATE_OK;
  103. }
  104. else if(vars_to_check==CHECK_CPULOAD){
  105. if (check_value_list==TRUE) {
  106. if (strtolarray(&lvalue_list,value_list,",")==TRUE) {
  107. // -l parameters is present with only integers
  108. return_code=STATE_OK;
  109. temp_string = strscpy(temp_string,"CPU Load");
  110. while (lvalue_list[0+offset]>0 && lvalue_list[0+offset]<=17280 &&
  111. lvalue_list[1+offset]>=0 && lvalue_list[1+offset]<=100 &&
  112. lvalue_list[2+offset]>=0 && lvalue_list[2+offset]<=100) {
  113. // loop until one of the parameters is wrong or not present
  114. // Send request and retrieve data
  115. send_buffer = ssprintf(send_buffer,"%s&2&%lu",req_password,lvalue_list[0+offset]);
  116. result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
  117. if(result!=STATE_OK)
  118. return result;
  119. if (!strncmp(recv_buffer,"ERROR",5)) {
  120. printf("NSClient - %s\n",recv_buffer);
  121. exit(STATE_UNKNOWN);
  122. }
  123. utilization=strtoul(recv_buffer,NULL,10);
  124. // Check if any of the request is in a warning or critical state
  125. if(utilization >= lvalue_list[2+offset])
  126. return_code=STATE_CRITICAL;
  127. else if(utilization >= lvalue_list[1+offset] && return_code<STATE_WARNING)
  128. return_code=STATE_WARNING;
  129. output_message = ssprintf(output_message," (%lu min. %lu%)",lvalue_list[0+offset], utilization);
  130. temp_string = strscat(temp_string,output_message);
  131. offset+=3; //move accross the array
  132. }
  133. if (strlen(temp_string)>10) {
  134. // we had at least on loop
  135. output_message = ssprintf(output_message,"%s",temp_string);
  136. }
  137. else
  138. output_message = strscpy(output_message,"not enough values for -l parameters");
  139. } else
  140. output_message = strscpy(output_message,"wrong -l parameter.");
  141. } else
  142. output_message = strscpy(output_message,"missing -l parameters");
  143. }
  144. else if(vars_to_check==CHECK_UPTIME){
  145. send_buffer = strscpy(send_buffer,strcat(req_password,"&3"));
  146. result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
  147. if(result!=STATE_OK)
  148. return result;
  149. if (!strncmp(recv_buffer,"ERROR",5)) {
  150. printf("NSClient - %s\n",recv_buffer);
  151. exit(STATE_UNKNOWN);
  152. }
  153. uptime=strtoul(recv_buffer,NULL,10);
  154. updays = uptime / 86400;
  155. uphours = (uptime % 86400) / 3600;
  156. upminutes = ((uptime % 86400) % 3600) / 60;
  157. output_message = ssprintf(output_message,"System Uptime : %u day(s) %u hour(s) %u minute(s)",updays,uphours, upminutes);
  158. return_code=STATE_OK;
  159. }
  160. else if(vars_to_check==CHECK_USEDDISKSPACE){
  161. return_code=STATE_UNKNOWN;
  162. if (check_value_list==TRUE) {
  163. if (strlen(value_list)==1) {
  164. send_buffer = ssprintf(send_buffer,"%s&4&%s", req_password, value_list);
  165. result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
  166. if(result!=STATE_OK)
  167. return result;
  168. if (!strncmp(recv_buffer,"ERROR",5)) {
  169. printf("NSClient - %s\n",recv_buffer);
  170. exit(STATE_UNKNOWN);
  171. }
  172. free_disk_space=atof(strtok(recv_buffer,"&"));
  173. total_disk_space=atof(strtok(NULL,"&"));
  174. percent_used_space = ((total_disk_space - free_disk_space) / total_disk_space) * 100;
  175. if (free_disk_space>=0) {
  176. temp_string = ssprintf(temp_string,"%s:\\ - total: %.2f Gb - used: %.2f Gb (%.0f%%) - free %.2f Gb (%.0f%%)",
  177. value_list, total_disk_space / 1073741824, (total_disk_space - free_disk_space) / 1073741824, percent_used_space,
  178. free_disk_space / 1073741824, (free_disk_space / total_disk_space)*100);
  179. if(check_critical_value==TRUE && percent_used_space >= critical_value)
  180. return_code=STATE_CRITICAL;
  181. else if (check_warning_value==TRUE && percent_used_space >= warning_value)
  182. return_code=STATE_WARNING;
  183. else
  184. return_code=STATE_OK;
  185. output_message = ssprintf(output_message,"%s",temp_string);
  186. }
  187. else {
  188. output_message = ssprintf(output_message,"Free disk space : Invalid drive ");
  189. return_code=STATE_UNKNOWN;
  190. }
  191. }
  192. else
  193. output_message = strscpy(output_message,"wrong -l argument");
  194. } else
  195. output_message = strscpy(output_message,"missing -l parameters");
  196. }
  197. else if(vars_to_check==CHECK_SERVICESTATE || vars_to_check==CHECK_PROCSTATE){
  198. if (check_value_list==TRUE) {
  199. preparelist(value_list); // replace , between services with & to send the request
  200. send_buffer = ssprintf(send_buffer,"%s&%u&%s&%s", req_password,(vars_to_check==CHECK_SERVICESTATE)?5:6,
  201. (show_all==TRUE)?"ShowAll":"ShowFail",value_list);
  202. result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
  203. if(result!=STATE_OK)
  204. return result;
  205. if (!strncmp(recv_buffer,"ERROR",5)) {
  206. printf("NSClient - %s\n",recv_buffer);
  207. exit(STATE_UNKNOWN);
  208. }
  209. return_code=atoi(strtok(recv_buffer,"&"));
  210. temp_string=strtok(NULL,"&");
  211. output_message = ssprintf(output_message, "%s",temp_string);
  212. }
  213. else
  214. output_message = strscpy(output_message,"No service/process specified");
  215. }
  216. else if(vars_to_check==CHECK_MEMUSE) {
  217. send_buffer = ssprintf(send_buffer,"%s&7", req_password);
  218. result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
  219. if (result!=STATE_OK)
  220. return result;
  221. if (!strncmp(recv_buffer,"ERROR",5)) {
  222. printf("NSClient - %s\n",recv_buffer);
  223. exit(STATE_UNKNOWN);
  224. }
  225. mem_commitLimit=atof(strtok(recv_buffer,"&"));
  226. mem_commitByte=atof(strtok(NULL,"&"));
  227. percent_used_space = (mem_commitByte / mem_commitLimit) * 100;
  228. output_message = ssprintf(output_message,"Memory usage: total:%.2f Mb - used: %.2f Mb (%.0f%%) - free: %.2f Mb (%.0f%%)",
  229. mem_commitLimit / 1048576, mem_commitByte / 1048567, percent_used_space,
  230. (mem_commitLimit - mem_commitByte) / 1048576, (mem_commitLimit - mem_commitByte) / mem_commitLimit * 100);
  231. if(check_critical_value==TRUE && percent_used_space >= critical_value)
  232. return_code=STATE_CRITICAL;
  233. else if (check_warning_value==TRUE && percent_used_space >= warning_value)
  234. return_code=STATE_WARNING;
  235. else
  236. return_code=STATE_OK;
  237. }
  238. else if(vars_to_check==CHECK_COUNTER) {
  239. if (check_value_list==TRUE) {
  240. preparelist(value_list); // replace , between services with & to send the request
  241. send_buffer = ssprintf(send_buffer,"%s&8&%s", req_password,value_list);
  242. result=process_tcp_request(server_address,server_port,send_buffer,recv_buffer,sizeof(recv_buffer));
  243. if (result!=STATE_OK)
  244. return result;
  245. if (!strncmp(recv_buffer,"ERROR",5)) {
  246. printf("NSClient - %s\n",recv_buffer);
  247. exit(STATE_UNKNOWN);
  248. }
  249. strtok(value_list,"&"); // burn the first parameters
  250. description = strtok(NULL,"&");
  251. counter_value = atof(recv_buffer);
  252. if (description == NULL)
  253. output_message = ssprintf(output_message, "%.f", counter_value);
  254. else
  255. output_message = ssprintf(output_message, description, counter_value);
  256. if (critical_value > warning_value) {
  257. // Normal thresholds
  258. if(check_critical_value==TRUE && counter_value >= critical_value)
  259. return_code=STATE_CRITICAL;
  260. else if (check_warning_value==TRUE && counter_value >= warning_value)
  261. return_code=STATE_WARNING;
  262. else
  263. return_code=STATE_OK;
  264. }
  265. else {
  266. // inverse thresholds
  267. if(check_critical_value==TRUE && counter_value <= critical_value)
  268. return_code=STATE_CRITICAL;
  269. else if (check_warning_value==TRUE && counter_value <= warning_value)
  270. return_code=STATE_WARNING;
  271. else
  272. return_code=STATE_OK;
  273. }
  274. }
  275. else {
  276. output_message = strscpy(output_message,"No counter specified");
  277. result=STATE_UNKNOWN;
  278. }
  279. }
  280. /* reset timeout */
  281. alarm(0);
  282. printf("%s\n",output_message);
  283. return return_code;
  284. }
  285. /* process command-line arguments */
  286. int process_arguments(int argc, char **argv){
  287. int c;
  288. #ifdef HAVE_GETOPT_H
  289. int option_index = 0;
  290. static struct option long_options[] =
  291. {
  292. {"port", required_argument,0,'p'},
  293. {"timeout", required_argument,0,'t'},
  294. {"critical", required_argument,0,'c'},
  295. {"warning", required_argument,0,'w'},
  296. {"variable", required_argument,0,'v'},
  297. {"hostname", required_argument,0,'H'},
  298. {"version", no_argument, 0,'V'},
  299. {"help", no_argument, 0,'h'},
  300. {0,0,0,0}
  301. };
  302. #endif
  303. /* no options were supplied */
  304. if(argc<2) return ERROR;
  305. /* backwards compatibility */
  306. if (! is_option(argv[1])) {
  307. server_address=argv[1];
  308. argv[1]=argv[0];
  309. argv=&argv[1];
  310. argc--;
  311. }
  312. for (c=1;c<argc;c++) {
  313. if(strcmp("-to",argv[c])==0)
  314. strcpy(argv[c],"-t");
  315. else if (strcmp("-wv",argv[c])==0)
  316. strcpy(argv[c],"-w");
  317. else if (strcmp("-cv",argv[c])==0)
  318. strcpy(argv[c],"-c");
  319. }
  320. while (1){
  321. #ifdef HAVE_GETOPT_H
  322. c = getopt_long(argc,argv,"+hVH:t:c:w:p:v:l:s:d:",long_options,&option_index);
  323. #else
  324. c = getopt(argc,argv,"+hVH:t:c:w:p:v:l:s:d:");
  325. #endif
  326. if (c==-1||c==EOF||c==1)
  327. break;
  328. switch (c)
  329. {
  330. case '?': /* print short usage statement if args not parsable */
  331. printf("%s: Unknown argument: %s\n\n",my_basename(argv[0]),optarg);
  332. print_usage();
  333. exit(STATE_UNKNOWN);
  334. case 'h': /* help */
  335. print_help();
  336. exit(STATE_OK);
  337. case 'V': /* version */
  338. print_revision(my_basename(argv[0]),"$Revision$");
  339. exit(STATE_OK);
  340. case 'H': /* hostname */
  341. server_address=optarg;
  342. break;
  343. case 's': /* password */
  344. req_password=strscpy(req_password,optarg);
  345. break;
  346. case 'p': /* port */
  347. if (is_intnonneg(optarg))
  348. server_port=atoi(optarg);
  349. else
  350. terminate(STATE_UNKNOWN,"Server port an integer (seconds)\nType '%s -h' for additional help\n",PROGNAME);
  351. break;
  352. case 'v':
  353. if(strlen(optarg)<4)
  354. return ERROR;
  355. if(!strcmp(optarg,"CLIENTVERSION"))
  356. vars_to_check=CHECK_CLIENTVERSION;
  357. else if(!strcmp(optarg,"CPULOAD"))
  358. vars_to_check=CHECK_CPULOAD;
  359. else if(!strcmp(optarg,"UPTIME"))
  360. vars_to_check=CHECK_UPTIME;
  361. else if(!strcmp(optarg,"USEDDISKSPACE"))
  362. vars_to_check=CHECK_USEDDISKSPACE;
  363. else if(!strcmp(optarg,"SERVICESTATE"))
  364. vars_to_check=CHECK_SERVICESTATE;
  365. else if(!strcmp(optarg,"PROCSTATE"))
  366. vars_to_check=CHECK_PROCSTATE;
  367. else if(!strcmp(optarg,"MEMUSE"))
  368. vars_to_check=CHECK_MEMUSE;
  369. else if(!strcmp(optarg,"COUNTER"))
  370. vars_to_check=CHECK_COUNTER;
  371. else
  372. return ERROR;
  373. break;
  374. case 'l': /* value list */
  375. value_list=strscpy(value_list,optarg);
  376. check_value_list=TRUE;
  377. break;
  378. case 'w': /* warning threshold */
  379. warning_value=strtoul(optarg,NULL,10);
  380. check_warning_value=TRUE;
  381. break;
  382. case 'c': /* critical threshold */
  383. critical_value=strtoul(optarg,NULL,10);
  384. check_critical_value=TRUE;
  385. break;
  386. case 'd': /* Display select for services */
  387. if (!strcmp(optarg,"SHOWALL"))
  388. show_all = TRUE;
  389. break;
  390. case 't': /* timeout */
  391. socket_timeout=atoi(optarg);
  392. if(socket_timeout<=0)
  393. return ERROR;
  394. }
  395. }
  396. return OK;
  397. }
  398. void print_usage(void)
  399. {
  400. printf("Usage: %s -H host [-p port] [-v variable] [-w warning] [-c critical] [-l params] [-d SHOWALL] [-t timeout]\n",PROGNAME);
  401. }
  402. void print_help(void)
  403. {
  404. print_revision(PROGNAME,"$Revision$");
  405. printf
  406. ("Copyright (c) 2000 Yves Rubin (rubiyz@yahoo.com)\n\n"
  407. "This plugin attempts to contact the NSClient service running on a Windows NT or Windows 2000 server to\n"
  408. "gather the requested system information.\n\n");
  409. print_usage();
  410. printf
  411. ("\nOptions:\n"
  412. "-H, --hostname=HOST\n"
  413. " Name of the host to check\n"
  414. "-p, --port=INTEGER\n"
  415. " Optional port number (default: %d)\n"
  416. "-s <password>\n"
  417. " Password needed for the request\n"
  418. "-v, --variable=STRING\n"
  419. " Variable to check. Valid variables are:\n"
  420. " CLIENTVERSION = Get the NSClient version\n"
  421. " CPULOAD = Average CPU load on last x minutes. Request a -l parameter with the following syntax:\n"
  422. " -l <minutes range>,<warning threshold>,<critical threshold>. <minute range> should be less than 24*60.\n"
  423. " Thresholds are percentage and up to 10 requests can be done in one shot. ie: -l 60,90,95,120,90,95\n"
  424. " UPTIME = Get the uptime of the machine. No specific parameters. No warning or critical threshold\n"
  425. " USEDDISKSPACE = Size and percentage of disk use. Request a -l parameter containing the drive letter only.\n"
  426. " Warning and critical thresholds can be specified with -w and -c.\n"
  427. " MEMUSE = Memory use. Warning and critical thresholds can be specified with -w and -c.\n"
  428. " SERVICESTATE = Check the state of one or several services. Request a -l parameters with the following syntax:\n"
  429. " -l <service1>,<service2>,<service3>,... You can specify -d SHOWALL in case you want to see working services\n"
  430. " in the returned string.\n"
  431. " PROCSTATE = Check if one or several process are running. Same syntax as SERVICESTATE.\n"
  432. " COUNTER = Check any performance counter of Windows NT/2000. Request a -l parameters with the following syntax:\n"
  433. " -l \"\\\\<performance object>\\\\counter\",\"<description>\" The <description> parameter is optional and \n"
  434. " is given to a printf output command which require a float parameters. Some examples:\n"
  435. " \"Paging file usage is %%.2f %%%%\" or \"%%.f %%%% paging file used.\"\n"
  436. " -w, --warning=INTEGER\n"
  437. " Threshold which will result in a warning status\n"
  438. " -c, --critical=INTEGER\n"
  439. " Threshold which will result in a critical status\n"
  440. " -t, --timeout=INTEGER\n"
  441. " Seconds before connection attempt times out (default: %d)\n"
  442. "-h, --help\n"
  443. " Print this help screen\n"
  444. "-V, --version\n"
  445. " Print version information\n\n"
  446. "Notes:\n"
  447. " - The NSClient service should be running on the server to get any information.\n"
  448. " - Critical thresholds should be lower than warning thresholds\n", PORT, DEFAULT_SOCKET_TIMEOUT);
  449. }
  450. int strtolarray(unsigned long *array, char *string, char *delim) {
  451. // split a <delim> delimited string into a long array
  452. int idx=0;
  453. char *t1;
  454. for (idx=0;idx<MAX_VALUE_LIST;idx++)
  455. array[idx]=-1;
  456. idx=0;
  457. for(t1 = strtok(string,delim);t1 != NULL; t1 = strtok(NULL, delim)) {
  458. if (is_numeric(t1) && idx<MAX_VALUE_LIST) {
  459. array[idx]=strtoul(t1,NULL,10);
  460. idx++;
  461. } else
  462. return FALSE;
  463. }
  464. return TRUE;
  465. }
  466. void preparelist(char *string) {
  467. // Replace all , with & which is the delimiter for the request
  468. int i;
  469. for (i = 0; i < strlen(string); i++)
  470. if (string[i] == ',') {
  471. string[i]='&';
  472. }
  473. }