1 /**
2 * collectd - src/irq.c
3 * Copyright (C) 2007 Peter Holik
4 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Peter Holik <peter at holik.at>
21 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
28 #if !KERNEL_LINUX
29 # error "No applicable input method."
30 #endif
32 #define BUFSIZE 128
34 /*
35 * (Module-)Global variables
36 */
37 static const char *config_keys[] =
38 {
39 "Irq",
40 "IgnoreSelected"
41 };
42 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
44 static unsigned int *irq_list;
45 static unsigned int irq_list_num;
47 /*
48 * irq_list_action:
49 * 0 => default is to collect selected irqs
50 * 1 => ignore selcted irqs
51 */
52 static int irq_list_action;
54 static int irq_config (const char *key, const char *value)
55 {
56 if (strcasecmp (key, "Irq") == 0)
57 {
58 unsigned int *temp;
59 unsigned int irq;
60 char *endptr;
62 temp = (unsigned int *) realloc (irq_list, (irq_list_num + 1) * sizeof (unsigned int *));
63 if (temp == NULL)
64 {
65 fprintf (stderr, "irq plugin: Cannot allocate more memory.\n");
66 ERROR ("irq plugin: Cannot allocate more memory.");
67 return (1);
68 }
69 irq_list = temp;
71 /* Clear errno, because we need it to see if an error occured. */
72 errno = 0;
74 irq = strtol(value, &endptr, 10);
75 if ((endptr == value) || (errno != 0))
76 {
77 fprintf (stderr, "irq plugin: Irq value is not a "
78 "number: `%s'\n", value);
79 ERROR ("irq plugin: Irq value is not a "
80 "number: `%s'", value);
81 return (1);
82 }
83 irq_list[irq_list_num] = irq;
84 irq_list_num++;
85 }
86 else if (strcasecmp (key, "IgnoreSelected") == 0)
87 {
88 if (IS_TRUE (value))
89 irq_list_action = 1;
90 else
91 irq_list_action = 0;
92 }
93 else
94 {
95 return (-1);
96 }
97 return (0);
98 }
100 /*
101 * Check if this interface/instance should be ignored. This is called from
102 * both, `submit' and `write' to give client and server the ability to
103 * ignore certain stuff..
104 */
105 static int check_ignore_irq (const unsigned int irq)
106 {
107 int i;
109 if (irq_list_num < 1)
110 return (0);
112 for (i = 0; (unsigned int)i < irq_list_num; i++)
113 if (irq == irq_list[i])
114 return (irq_list_action);
116 return (1 - irq_list_action);
117 }
119 static void irq_submit (unsigned int irq, counter_t value)
120 {
121 value_t values[1];
122 value_list_t vl = VALUE_LIST_INIT;
123 int status;
125 if (check_ignore_irq (irq))
126 return;
128 values[0].counter = value;
130 vl.values = values;
131 vl.values_len = 1;
132 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
133 sstrncpy (vl.plugin, "irq", sizeof (vl.plugin));
134 sstrncpy (vl.type, "irq", sizeof (vl.type));
136 status = ssnprintf (vl.type_instance, sizeof (vl.type_instance),
137 "%u", irq);
138 if ((status < 1) || ((unsigned int)status >= sizeof (vl.type_instance)))
139 return;
141 plugin_dispatch_values (&vl);
142 } /* void irq_submit */
144 static int irq_read (void)
145 {
146 #undef BUFSIZE
147 #define BUFSIZE 256
149 FILE *fh;
150 char buffer[BUFSIZE];
151 unsigned int irq;
152 unsigned long long irq_value;
153 unsigned long long value;
154 char *endptr;
155 int i;
157 char *fields[64];
158 int fields_num;
160 if ((fh = fopen ("/proc/interrupts", "r")) == NULL)
161 {
162 char errbuf[1024];
163 WARNING ("irq plugin: fopen (/proc/interrupts): %s",
164 sstrerror (errno, errbuf, sizeof (errbuf)));
165 return (-1);
166 }
167 while (fgets (buffer, BUFSIZE, fh) != NULL)
168 {
169 fields_num = strsplit (buffer, fields, 64);
170 if (fields_num < 2)
171 continue;
173 errno = 0; /* To distinguish success/failure after call */
174 irq = strtol (fields[0], &endptr, 10);
176 if ((endptr == fields[0]) || (errno != 0) || (*endptr != ':'))
177 continue;
179 irq_value = 0;
180 for (i = 1; i < fields_num; i++)
181 {
182 errno = 0;
183 value = strtoull (fields[i], &endptr, 10);
185 if ((*endptr != '\0') || (errno != 0))
186 break;
188 irq_value += value;
189 } /* for (i) */
191 /* Force 32bit wrap-around */
192 irq_submit (irq, irq_value % 4294967296ULL);
193 }
195 fclose (fh);
197 return (0);
198 } /* int irq_read */
200 void module_register (void)
201 {
202 plugin_register_config ("irq", irq_config,
203 config_keys, config_keys_num);
204 plugin_register_read ("irq", irq_read);
205 } /* void module_register */
207 #undef BUFSIZE