check_mrtgtraf.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /******************************************************************************
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  13. ******************************************************************************/
  14. #include "common.h"
  15. #include "utils.h"
  16. const char *progname = "check_mrtgtraf";
  17. const char *revision = "$Revision$";
  18. const char *copyright = "1999-2003";
  19. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  20. void
  21. print_usage (void)
  22. {
  23. printf (_("\
  24. Usage: %s -F <log_file> -a <AVG | MAX> -v <variable> -w <warning_pair> -c <critical_pair>\n\
  25. [-e expire_minutes] [-t timeout] [-v]\n"), progname);
  26. printf (_(UT_HLP_VRS), progname, progname);
  27. }
  28. void
  29. print_help (void)
  30. {
  31. print_revision (progname, revision);
  32. printf (_(COPYRIGHT), copyright, email);
  33. print_usage ();
  34. printf (_(UT_HELP_VRSN));
  35. printf (_("\
  36. -F, --filename=STRING\n\
  37. File to read log from\n\
  38. -e, --expires=INTEGER\n\
  39. Minutes after which log expires\n\
  40. -a, --aggregation=(AVG|MAX)\n\
  41. Test average or maximum\n\
  42. -w, --warning\n\
  43. Warning threshold pair \"<incoming>,<outgoing>\"\n\
  44. -c, --critical\n\
  45. Critical threshold pair \"<incoming>,<outgoing>\"\n"));
  46. printf (_("\n\
  47. This plugin will check the incoming/outgoing transfer rates of a router,\n\
  48. switch, etc recorded in an MRTG log. If the newest log entry is older\n\
  49. than <expire_minutes>, a WARNING status is returned. If either the\n\
  50. incoming or outgoing rates exceed the <icl> or <ocl> thresholds (in\n\
  51. Bytes/sec), a CRITICAL status results. If either of the rates exceed\n\
  52. the <iwl> or <owl> thresholds (in Bytes/sec), a WARNING status results.\n\n"));
  53. printf (_("Notes:\n\
  54. - MRTG stands for Multi Router Traffic Grapher. It can be downloaded from\n\
  55. http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html\n\
  56. - While MRTG can monitor things other than traffic rates, this\n\
  57. plugin probably won't work with much else without modification.\n\
  58. - The calculated i/o rates are a little off from what MRTG actually\n\
  59. reports. I'm not sure why this is right now, but will look into it\n\
  60. for future enhancements of this plugin.\n"));
  61. printf (_(UT_SUPPORT));
  62. }
  63. int process_arguments (int, char **);
  64. int validate_arguments (void);
  65. char *log_file = NULL;
  66. int expire_minutes = -1;
  67. int use_average = TRUE;
  68. unsigned long incoming_warning_threshold = 0L;
  69. unsigned long incoming_critical_threshold = 0L;
  70. unsigned long outgoing_warning_threshold = 0L;
  71. unsigned long outgoing_critical_threshold = 0L;
  72. int
  73. main (int argc, char **argv)
  74. {
  75. int result = STATE_OK;
  76. FILE *fp;
  77. int line;
  78. char input_buffer[MAX_INPUT_BUFFER];
  79. char *temp_buffer;
  80. time_t current_time;
  81. char *error_message;
  82. time_t timestamp = 0L;
  83. unsigned long average_incoming_rate = 0L;
  84. unsigned long average_outgoing_rate = 0L;
  85. unsigned long maximum_incoming_rate = 0L;
  86. unsigned long maximum_outgoing_rate = 0L;
  87. unsigned long incoming_rate = 0L;
  88. unsigned long outgoing_rate = 0L;
  89. double adjusted_incoming_rate = 0.0;
  90. double adjusted_outgoing_rate = 0.0;
  91. char incoming_speed_rating[8];
  92. char outgoing_speed_rating[8];
  93. if (process_arguments (argc, argv) != OK)
  94. usage (_("Invalid command arguments supplied\n"));
  95. /* open the MRTG log file for reading */
  96. fp = fopen (log_file, "r");
  97. if (fp == NULL)
  98. usage (_("Unable to open MRTG log file\n"));
  99. line = 0;
  100. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  101. line++;
  102. /* skip the first line of the log file */
  103. if (line == 1)
  104. continue;
  105. /* break out of read loop */
  106. /* if we've passed the number of entries we want to read */
  107. if (line > 2)
  108. break;
  109. /* grab the timestamp */
  110. temp_buffer = strtok (input_buffer, " ");
  111. timestamp = strtoul (temp_buffer, NULL, 10);
  112. /* grab the average incoming transfer rate */
  113. temp_buffer = strtok (NULL, " ");
  114. average_incoming_rate = strtoul (temp_buffer, NULL, 10);
  115. /* grab the average outgoing transfer rate */
  116. temp_buffer = strtok (NULL, " ");
  117. average_outgoing_rate = strtoul (temp_buffer, NULL, 10);
  118. /* grab the maximum incoming transfer rate */
  119. temp_buffer = strtok (NULL, " ");
  120. maximum_incoming_rate = strtoul (temp_buffer, NULL, 10);
  121. /* grab the maximum outgoing transfer rate */
  122. temp_buffer = strtok (NULL, " ");
  123. maximum_outgoing_rate = strtoul (temp_buffer, NULL, 10);
  124. }
  125. /* close the log file */
  126. fclose (fp);
  127. /* if we couldn't read enough data, return an unknown error */
  128. if (line <= 2)
  129. usage (_("Unable to process MRTG log file\n"));
  130. /* make sure the MRTG data isn't too old */
  131. time (&current_time);
  132. if (expire_minutes > 0
  133. && (current_time - timestamp) >
  134. (expire_minutes * 60)) die (STATE_WARNING,
  135. _("MRTG data has expired (%d minutes old)\n"),
  136. (int) ((current_time - timestamp) /
  137. 60));
  138. /* else check the incoming/outgoing rates */
  139. if (use_average == TRUE) {
  140. incoming_rate = average_incoming_rate;
  141. outgoing_rate = average_outgoing_rate;
  142. }
  143. else {
  144. incoming_rate = maximum_incoming_rate;
  145. outgoing_rate = maximum_outgoing_rate;
  146. }
  147. /* report incoming traffic in Bytes/sec */
  148. if (incoming_rate < 1024) {
  149. strcpy (incoming_speed_rating, "B/s");
  150. adjusted_incoming_rate = (double) incoming_rate;
  151. }
  152. /* report incoming traffic in KBytes/sec */
  153. else if (incoming_rate < (1024 * 1024)) {
  154. strcpy (incoming_speed_rating, "KB/s");
  155. adjusted_incoming_rate = (double) (incoming_rate / 1024.0);
  156. }
  157. /* report incoming traffic in MBytes/sec */
  158. else {
  159. strcpy (incoming_speed_rating, "MB/s");
  160. adjusted_incoming_rate = (double) (incoming_rate / 1024.0 / 1024.0);
  161. }
  162. /* report outgoing traffic in Bytes/sec */
  163. if (outgoing_rate < 1024) {
  164. strcpy (outgoing_speed_rating, "B/s");
  165. adjusted_outgoing_rate = (double) outgoing_rate;
  166. }
  167. /* report outgoing traffic in KBytes/sec */
  168. else if (outgoing_rate < (1024 * 1024)) {
  169. strcpy (outgoing_speed_rating, "KB/s");
  170. adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0);
  171. }
  172. /* report outgoing traffic in MBytes/sec */
  173. else {
  174. strcpy (outgoing_speed_rating, "MB/s");
  175. adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0 / 1024.0);
  176. }
  177. if (incoming_rate > incoming_critical_threshold
  178. || outgoing_rate > outgoing_critical_threshold) {
  179. result = STATE_CRITICAL;
  180. asprintf (&error_message, _("Traffic CRITICAL %s. In = %0.1f %s, %s. Out = %0.1f %s"),
  181. (use_average == TRUE) ? _("Avg") : _("Max"), adjusted_incoming_rate,
  182. incoming_speed_rating, (use_average == TRUE) ? "Avg" : "Max",
  183. adjusted_outgoing_rate, outgoing_speed_rating);
  184. }
  185. else if (incoming_rate > incoming_warning_threshold
  186. || outgoing_rate > outgoing_warning_threshold) {
  187. result = STATE_WARNING;
  188. asprintf (&error_message, _("Traffic WARNING %s. In = %0.1f %s, %s. Out = %0.1f %s"),
  189. (use_average == TRUE) ? _("Avg") : _("Max"), adjusted_incoming_rate,
  190. incoming_speed_rating, (use_average == TRUE) ? _("Avg") : _("Max"),
  191. adjusted_outgoing_rate, outgoing_speed_rating);
  192. }
  193. else if (result == STATE_OK)
  194. printf (_("Traffic OK - %s. In = %0.1f %s, %s. Out = %0.1f %s\n"),
  195. (use_average == TRUE) ? _("Avg") : _("Max"), adjusted_incoming_rate,
  196. incoming_speed_rating, (use_average == TRUE) ? _("Avg") : _("Max"),
  197. adjusted_outgoing_rate, outgoing_speed_rating);
  198. else
  199. printf (_("UNKNOWN %s\n"), error_message);
  200. return result;
  201. }
  202. /* process command-line arguments */
  203. int
  204. process_arguments (int argc, char **argv)
  205. {
  206. int c;
  207. int option_index = 0;
  208. static struct option longopts[] = {
  209. {"logfile", required_argument, 0, 'F'},
  210. {"expires", required_argument, 0, 'e'},
  211. {"aggregation", required_argument, 0, 'a'},
  212. {"variable", required_argument, 0, 'v'},
  213. {"critical", required_argument, 0, 'c'},
  214. {"warning", required_argument, 0, 'w'},
  215. {"verbose", no_argument, 0, 'v'},
  216. {"version", no_argument, 0, 'V'},
  217. {"help", no_argument, 0, 'h'},
  218. {0, 0, 0, 0}
  219. };
  220. if (argc < 2)
  221. return ERROR;
  222. for (c = 1; c < argc; c++) {
  223. if (strcmp ("-to", argv[c]) == 0)
  224. strcpy (argv[c], "-t");
  225. else if (strcmp ("-wt", argv[c]) == 0)
  226. strcpy (argv[c], "-w");
  227. else if (strcmp ("-ct", argv[c]) == 0)
  228. strcpy (argv[c], "-c");
  229. }
  230. while (1) {
  231. c = getopt_long (argc, argv, "hVF:e:a:c:w:", longopts, &option_index);
  232. if (c == -1 || c == EOF)
  233. break;
  234. switch (c) {
  235. case 'F': /* input file */
  236. log_file = optarg;
  237. break;
  238. case 'e': /* expiration time */
  239. expire_minutes = atoi (optarg);
  240. break;
  241. case 'a': /* aggregation (AVE or MAX) */
  242. if (!strcmp (optarg, "MAX"))
  243. use_average = FALSE;
  244. else
  245. use_average = TRUE;
  246. break;
  247. case 'c': /* warning threshold */
  248. sscanf (optarg, "%lu,%lu", &incoming_critical_threshold,
  249. &outgoing_critical_threshold);
  250. break;
  251. case 'w': /* critical threshold */
  252. sscanf (optarg, "%lu,%lu", &incoming_warning_threshold,
  253. &outgoing_warning_threshold);
  254. break;
  255. case 'V': /* version */
  256. print_revision (progname, revision);
  257. exit (STATE_OK);
  258. case 'h': /* help */
  259. print_help ();
  260. exit (STATE_OK);
  261. case '?': /* help */
  262. usage (_("Invalid argument\n"));
  263. }
  264. }
  265. c = optind;
  266. if (argc > c && log_file == NULL) {
  267. log_file = argv[c++];
  268. }
  269. if (argc > c && expire_minutes == -1) {
  270. expire_minutes = atoi (argv[c++]);
  271. }
  272. if (argc > c && strcmp (argv[c], "MAX") == 0) {
  273. use_average = FALSE;
  274. c++;
  275. }
  276. else if (argc > c && strcmp (argv[c], "AVG") == 0) {
  277. use_average = TRUE;
  278. c++;
  279. }
  280. if (argc > c && incoming_warning_threshold == 0) {
  281. incoming_warning_threshold = strtoul (argv[c++], NULL, 10);
  282. }
  283. if (argc > c && incoming_critical_threshold == 0) {
  284. incoming_critical_threshold = strtoul (argv[c++], NULL, 10);
  285. }
  286. if (argc > c && outgoing_warning_threshold == 0) {
  287. outgoing_warning_threshold = strtoul (argv[c++], NULL, 10);
  288. }
  289. if (argc > c && outgoing_critical_threshold == 0) {
  290. outgoing_critical_threshold = strtoul (argv[c++], NULL, 10);
  291. }
  292. return validate_arguments ();
  293. }
  294. int
  295. validate_arguments (void)
  296. {
  297. return OK;
  298. }