2 * collectd - src/qmail.c
3 * Copyright (C) 2008 Alessandro Iurlano
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; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Alessandro Iurlano <alessandro.iurlano at gmail.com>
26 #include <sys/types.h>
31 #define DEFAULT_BASE_DIR "/var/qmail"
33 static char *qmail_base_dir;
35 static const char *config_keys[] =
39 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
41 static void qmail_submit (const char *plugin_instance, gauge_t value)
44 value_list_t vl = VALUE_LIST_INIT;
46 values[0].gauge = value;
49 vl.values_len = STATIC_ARRAY_SIZE (values);
50 vl.time = time (NULL);
51 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
52 sstrncpy (vl.type, "gauge", sizeof (vl.type));
53 sstrncpy (vl.plugin, "qmail", sizeof (vl.plugin));
54 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
56 plugin_dispatch_values (&vl);
57 } /* void qmail_submit */
59 static int count_files_in_subtree (const char *path, int depth)
74 ERROR ("qmail plugin: opendir (%s) failed.", path);
82 while ((de = readdir (dh)) != NULL)
87 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", path, de->d_name);
89 status = lstat (abs_path, &statbuf);
92 ERROR ("qmail plugin: stat (%s) failed.", abs_path);
96 if (S_ISREG (statbuf.st_mode))
100 else if (S_ISDIR (statbuf.st_mode))
104 temp = (char **) realloc (subdirs, sizeof (char *) * (subdirs_num + 1));
107 ERROR ("qmail plugin: realloc failed.");
112 subdirs[subdirs_num] = strdup (abs_path);
113 if (subdirs[subdirs_num] == NULL)
115 ERROR ("qmail plugin: strdup failed.");
127 for (i = 0; i < subdirs_num; i++)
129 status = count_files_in_subtree (subdirs[i], depth - 1);
135 for (i = 0; i < subdirs_num; i++)
142 } /* int count_files_in_subtree */
144 static int read_queue_length (const char *queue_name, const char *path)
148 num_files = count_files_in_subtree (path, /* depth = */ 1);
151 ERROR ("qmail plugin: Counting files in `%s' failed.", path);
155 qmail_submit (queue_name, (gauge_t) num_files);
157 } /* int read_queue_length */
159 static int queue_len_read (void)
167 ssnprintf (path, sizeof (path), "%s/queue/mess",
168 (qmail_base_dir != NULL)
172 status = read_queue_length ("messages", path);
176 ssnprintf (path, sizeof (path), "%s/queue/todo",
177 (qmail_base_dir != NULL)
181 status = read_queue_length ("todo", path);
188 } /* int queue_len_read */
190 static int qmail_config (const char *key, const char *val)
192 if (strcasecmp ("QmailDir", key) == 0)
194 size_t qmail_base_dir_len;
196 sfree (qmail_base_dir);
197 qmail_base_dir = strdup(val);
198 if (qmail_base_dir == NULL)
200 ERROR ("qmail plugin: strdup failed.");
204 qmail_base_dir_len = strlen (qmail_base_dir);
205 while ((qmail_base_dir_len > 0)
206 && (qmail_base_dir[qmail_base_dir_len - 1] == '/'))
208 qmail_base_dir[qmail_base_dir_len - 1] = 0;
209 qmail_base_dir_len--;
212 if (qmail_base_dir_len == 0)
214 ERROR ("qmail plugin: QmailDir is invalid.");
215 sfree (qmail_base_dir);
216 qmail_base_dir = NULL;
226 } /* int qmail_config */
228 void module_register (void)
230 plugin_register_config ("qmail", qmail_config,
231 config_keys, config_keys_num);
232 plugin_register_read ("qmail", queue_len_read);
233 } /* void module_register */
236 * vim: set sw=2 sts=2 et :