check_nt.c 17 KB

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