3 * Copyright (C) 2007 Peter Holik
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Peter Holik <peter at holik.at>
26 #include "configfile.h"
28 #define MODULE_NAME "irq"
31 # define IRQ_HAVE_READ 1
33 # define IRQ_HAVE_READ 0
39 * (Module-)Global variables
41 static char *irq_file = "irq/irq-%s.rrd";
43 static char *config_keys[] =
49 static int config_keys_num = 2;
51 static char *ds_def[] =
53 "DS:value:COUNTER:"COLLECTD_HEARTBEAT":0:65535",
56 static int ds_num = 1;
58 static unsigned int *irq_list;
59 static unsigned int irq_list_num;
63 * 0 => default is to collect selected irqs
64 * 1 => ignore selcted irqs
66 static int irq_list_action;
68 static int irq_config (char *key, char *value)
70 if (strcasecmp (key, "Irq") == 0)
76 temp = (unsigned int *) realloc (irq_list, (irq_list_num + 1) * sizeof (unsigned int *));
79 fprintf (stderr, "irq plugin: Cannot allocate more memory.\n");
80 syslog (LOG_ERR, "irq plugin: Cannot allocate more memory.");
85 /* Clear errno, because we need it to see if an error occured. */
88 irq = strtol(value, &endptr, 10);
89 if ((endptr == value) || (errno != 0))
91 fprintf (stderr, "irq plugin: Irq value is not a "
92 "number: `%s'\n", value);
93 syslog (LOG_ERR, "irq plugin: Irq value is not a "
94 "number: `%s'", value);
97 irq_list[irq_list_num] = irq;
100 else if (strcasecmp (key, "IgnoreSelected") == 0)
102 if ((strcasecmp (value, "True") == 0)
103 || (strcasecmp (value, "Yes") == 0)
104 || (strcasecmp (value, "On") == 0))
117 * Check if this interface/instance should be ignored. This is called from
118 * both, `submit' and `write' to give client and server the ability to
119 * ignore certain stuff..
121 static int check_ignore_irq (const unsigned int irq)
125 if (irq_list_num < 1)
128 for (i = 0; i < irq_list_num; i++)
129 if (irq == irq_list[i])
130 return (irq_list_action);
132 return (1 - irq_list_action);
135 static void irq_write (char *host, char *inst, char *value)
140 if (check_ignore_irq (atoi(inst)))
143 status = snprintf (file, BUFSIZE, irq_file, inst);
146 else if (status >= BUFSIZE)
149 rrd_update_file (host, file, value, ds_def, ds_num);
153 static void irq_submit (unsigned int irq, unsigned int value)
159 if (check_ignore_irq (irq))
162 status = snprintf (value_str, 32, "%u:%u",
163 (unsigned int) curtime, value);
164 if ((status >= 32) || (status < 1))
167 status = snprintf (type_str, 16, "%u", irq);
168 if ((status >= 16) || (status < 1))
171 plugin_submit (MODULE_NAME, type_str, value_str);
172 } /* void irq_submit */
174 static void irq_read (void)
182 char buffer[BUFSIZE];
184 unsigned int irq_value;
192 if ((fh = fopen ("/proc/interrupts", "r")) == NULL)
194 syslog (LOG_WARNING, "irq plugin: fopen (/proc/interrupts): %s",
198 while (fgets (buffer, BUFSIZE, fh) != NULL)
200 fields_num = strsplit (buffer, fields, 64);
204 errno = 0; /* To distinguish success/failure after call */
205 irq = strtol (fields[0], &endptr, 10);
207 if ((endptr == fields[0]) || (errno != 0) || (*endptr != ':'))
211 for (i = 1; i < fields_num; i++)
214 value = strtol (fields[i], &endptr, 10);
216 if ((*endptr != '\0') || (errno != 0))
222 irq_submit (irq, irq_value);
225 #endif /* KERNEL_LINUX */
226 } /* void irq_read */
228 #define irq_read NULL
229 #endif /* IRQ_HAVE_READ */
231 void module_register (void)
233 plugin_register (MODULE_NAME, NULL, irq_read, irq_write);
234 cf_register (MODULE_NAME, irq_config, config_keys, config_keys_num);