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"
29 # error "No applicable input method."
33 * (Module-)Global variables
35 static const char *config_keys[] =
40 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
42 static char **irq_list;
43 static unsigned int irq_list_num = 0;
47 * 0 => default is to collect selected irqs
48 * 1 => ignore selcted irqs
50 static int irq_list_action;
52 static int irq_config (const char *key, const char *value)
54 if (strcasecmp (key, "Irq") == 0)
58 temp = realloc (irq_list, (irq_list_num + 1) * sizeof (*irq_list));
61 fprintf (stderr, "irq plugin: Cannot allocate more memory.\n");
62 ERROR ("irq plugin: Cannot allocate more memory.");
67 irq_list[irq_list_num] = strdup (value);
68 if (irq_list[irq_list_num] == NULL)
70 ERROR ("irq plugin: strdup(3) failed.");
76 else if (strcasecmp (key, "IgnoreSelected") == 0)
91 * Check if this interface/instance should be ignored. This is called from
92 * both, `submit' and `write' to give client and server the ability to
93 * ignore certain stuff..
95 static int check_ignore_irq (const char *irq)
102 for (i = 0; i < irq_list_num; i++)
103 if (strcmp (irq, irq_list[i]) == 0)
104 return (irq_list_action);
106 return (1 - irq_list_action);
109 static void irq_submit (const char *irq_name, counter_t value)
112 value_list_t vl = VALUE_LIST_INIT;
114 if (check_ignore_irq (irq_name))
117 values[0].counter = value;
121 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
122 sstrncpy (vl.plugin, "irq", sizeof (vl.plugin));
123 sstrncpy (vl.type, "irq", sizeof (vl.type));
124 sstrncpy (vl.type_instance, irq_name, sizeof (vl.type_instance));
126 plugin_dispatch_values (&vl);
127 } /* void irq_submit */
129 static int irq_read (void)
133 unsigned long long irq_value;
134 unsigned long long value;
141 if ((fh = fopen ("/proc/interrupts", "r")) == NULL)
144 WARNING ("irq plugin: fopen (/proc/interrupts): %s",
145 sstrerror (errno, errbuf, sizeof (errbuf)));
148 while (fgets (buffer, sizeof (buffer), fh) != NULL)
153 fields_num = strsplit (buffer, fields, 64);
157 irq_name = fields[0];
158 irq_name_len = strlen (irq_name);
159 if (irq_name_len < 2)
162 /* Check if irq name ends with colon.
163 * Otherwise it's a header. */
164 if (irq_name[irq_name_len - 1] != ':')
167 irq_name[irq_name_len - 1] = 0;
171 for (i = 1; i < fields_num; i++)
175 value = strtoull (fields[i], &endptr, 10);
177 /* Ignore all fields following a non-numeric field. */
178 if ((errno != 0) || (endptr == NULL) || (*endptr != 0))
184 /* No valid fields -> do not submit anything. */
188 /* Force 32bit wrap-around */
189 irq_submit (irq_name, irq_value % 4294967296ULL);
197 void module_register (void)
199 plugin_register_config ("irq", irq_config,
200 config_keys, config_keys_num);
201 plugin_register_read ("irq", irq_read);
202 } /* void module_register */