check_nt.c 17 KB

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