6c30e95a752bb4583ca6ce466279062cdfb7e094
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 #define MODULE_NAME "irq"
30 #if KERNEL_LINUX
31 # define IRQ_HAVE_READ 1
32 #else
33 # define IRQ_HAVE_READ 0
34 #endif
36 #define BUFSIZE 128
38 /*
39 * (Module-)Global variables
40 */
41 static data_source_t dsrc_irq[1] =
42 {
43 {"value", DS_TYPE_COUNTER, 0, 65535.0}
44 };
46 static data_set_t ds_irq =
47 {
48 "irq", 1, dsrc_irq
49 };
51 #if IRQ_HAVE_READ
52 static const char *config_keys[] =
53 {
54 "Irq",
55 "IgnoreSelected"
56 };
57 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
59 static unsigned int *irq_list;
60 static unsigned int irq_list_num;
62 /*
63 * irq_list_action:
64 * 0 => default is to collect selected irqs
65 * 1 => ignore selcted irqs
66 */
67 static int irq_list_action;
69 static int irq_config (const char *key, const char *value)
70 {
71 if (strcasecmp (key, "Irq") == 0)
72 {
73 unsigned int *temp;
74 unsigned int irq;
75 char *endptr;
77 temp = (unsigned int *) realloc (irq_list, (irq_list_num + 1) * sizeof (unsigned int *));
78 if (temp == NULL)
79 {
80 fprintf (stderr, "irq plugin: Cannot allocate more memory.\n");
81 syslog (LOG_ERR, "irq plugin: Cannot allocate more memory.");
82 return (1);
83 }
84 irq_list = temp;
86 /* Clear errno, because we need it to see if an error occured. */
87 errno = 0;
89 irq = strtol(value, &endptr, 10);
90 if ((endptr == value) || (errno != 0))
91 {
92 fprintf (stderr, "irq plugin: Irq value is not a "
93 "number: `%s'\n", value);
94 syslog (LOG_ERR, "irq plugin: Irq value is not a "
95 "number: `%s'", value);
96 return (1);
97 }
98 irq_list[irq_list_num] = irq;
99 irq_list_num++;
100 }
101 else if (strcasecmp (key, "IgnoreSelected") == 0)
102 {
103 if ((strcasecmp (value, "True") == 0)
104 || (strcasecmp (value, "Yes") == 0)
105 || (strcasecmp (value, "On") == 0))
106 irq_list_action = 1;
107 else
108 irq_list_action = 0;
109 }
110 else
111 {
112 return (-1);
113 }
114 return (0);
115 }
117 /*
118 * Check if this interface/instance should be ignored. This is called from
119 * both, `submit' and `write' to give client and server the ability to
120 * ignore certain stuff..
121 */
122 static int check_ignore_irq (const unsigned int irq)
123 {
124 int i;
126 if (irq_list_num < 1)
127 return (0);
129 for (i = 0; i < irq_list_num; i++)
130 if (irq == irq_list[i])
131 return (irq_list_action);
133 return (1 - irq_list_action);
134 }
136 static void irq_submit (unsigned int irq, counter_t value)
137 {
138 value_t values[1];
139 value_list_t vl = VALUE_LIST_INIT;
140 int status;
142 if (check_ignore_irq (irq))
143 return;
145 values[0].counter = value;
147 vl.values = values;
148 vl.values_len = 1;
149 vl.time = time (NULL);
150 strcpy (vl.host, hostname_g);
151 strcpy (vl.plugin, "irq");
153 status = snprintf (vl.type_instance, sizeof (vl.type_instance),
154 "%u", irq);
155 if ((status < 1) || (status >= sizeof (vl.type_instance)))
156 return;
158 plugin_dispatch_values ("irq", &vl);
159 } /* void irq_submit */
161 static int irq_read (void)
162 {
163 #if KERNEL_LINUX
165 #undef BUFSIZE
166 #define BUFSIZE 256
168 FILE *fh;
169 char buffer[BUFSIZE];
170 unsigned int irq;
171 unsigned int irq_value;
172 long value;
173 char *endptr;
174 int i;
176 char *fields[64];
177 int fields_num;
179 if ((fh = fopen ("/proc/interrupts", "r")) == NULL)
180 {
181 syslog (LOG_WARNING, "irq plugin: fopen (/proc/interrupts): %s",
182 strerror (errno));
183 return (-1);
184 }
185 while (fgets (buffer, BUFSIZE, fh) != NULL)
186 {
187 fields_num = strsplit (buffer, fields, 64);
188 if (fields_num < 2)
189 continue;
191 errno = 0; /* To distinguish success/failure after call */
192 irq = strtol (fields[0], &endptr, 10);
194 if ((endptr == fields[0]) || (errno != 0) || (*endptr != ':'))
195 continue;
197 irq_value = 0;
198 for (i = 1; i < fields_num; i++)
199 {
200 errno = 0;
201 value = strtol (fields[i], &endptr, 10);
203 if ((*endptr != '\0') || (errno != 0))
204 break;
206 irq_value += value;
207 } /* for (i) */
209 irq_submit (irq, irq_value);
210 }
211 fclose (fh);
212 #endif /* KERNEL_LINUX */
214 return (0);
215 } /* int irq_read */
216 #endif /* IRQ_HAVE_READ */
218 void module_register (void)
219 {
220 plugin_register_data_set (&ds_irq);
222 #if IRQ_HAVE_READ
223 plugin_register_config ("irq", irq_config,
224 config_keys, config_keys_num);
225 plugin_register_read ("irq", irq_read);
226 #endif /* IRQ_HAVE_READ */
227 }
229 #undef BUFSIZE
230 #undef MODULE_NAME