check_nt.c 17 KB

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