check_nt.c 18 KB

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