check_mrtgtraf.c 11 KB

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