check_mrtgtraf.c 10 KB

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