4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; only version 2 of the License is applicable.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Alessandro Iurlano <alessandro.iurlano@gmail.com>
25 #include <sys/types.h>
32 static char * qmail_base_dir;
34 static const char *config_keys[] =
38 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
41 static void qmail_queue_submit (gauge_t queued_messages, gauge_t todo_messages)
44 value_list_t vl = VALUE_LIST_INIT;
46 values[0].gauge = queued_messages;
49 vl.values_len = STATIC_ARRAY_SIZE (values);
50 vl.time = time (NULL);
51 strcpy (vl.host, hostname_g);
52 strcpy (vl.plugin, "qmail_queue");
53 strncpy (vl.plugin_instance, "messages",
54 sizeof (vl.plugin_instance));
57 plugin_dispatch_values ("gauge", &vl);
59 values[0].gauge = todo_messages;
62 vl.values_len = STATIC_ARRAY_SIZE (values);
63 vl.time = time (NULL);
64 strcpy (vl.host, hostname_g);
65 strcpy (vl.plugin, "qmail_queue");
66 strncpy (vl.plugin_instance, "todo",
67 sizeof (vl.plugin_instance));
69 plugin_dispatch_values ("gauge", &vl);
72 #define MAX_PATH_LEN 4096
74 static int count_files_in_dir(char * path) {
75 char *buf, *ebuf, *cp;
83 if ((fd = open(path, O_RDONLY)) < 0) {
84 ERROR("cannot open %s", path);
87 if (fstat(fd, &sb) < 0) {
92 if (bufsize < sb.st_blksize)
93 bufsize = sb.st_blksize;
94 if ((buf = malloc(bufsize)) == NULL) {
95 ERROR("cannot malloc %lu bytes", (unsigned long)bufsize);
98 while ((nbytes = getdirentries(fd, buf, bufsize, &base)) > 0) {
102 dp = (struct dirent *)cp;
103 if ( (dp->d_fileno!=0) && (dp->d_type!=DT_DIR) ) {
107 if (dp->d_fileno != 0)
108 printf("%s\n", dp->d_name);*/
113 ERROR("getdirentries");
121 static int count_files_in_tree(char * path) {
123 char *buf, *ebuf, *cp;
130 int path_len=strlen(path);
131 if ((fd = open(path, O_RDONLY)) < 0) {
132 WARNING("cannot open %s", path);
135 if (fstat(fd, &sb) < 0) {
139 bufsize = sb.st_size;
140 if (bufsize < sb.st_blksize)
141 bufsize = sb.st_blksize;
142 if ((buf = malloc(bufsize)) == NULL) {
143 ERROR("cannot malloc %lu bytes", (unsigned long)bufsize);
146 while ((nbytes = getdirentries(fd, buf, bufsize, &base)) > 0) {
151 dp = (struct dirent *)cp;
152 //WARNING("Looking file %s\n", dp->d_name);
153 if ((dp->d_fileno!=0) && (dp->d_type==DT_DIR) && strcmp(dp->d_name,".") && strcmp(dp->d_name,"..") ) {
154 snprintf(path+path_len,MAX_PATH_LEN-path_len,"%s",dp->d_name);
155 ret_value=count_files_in_dir(path);
157 files_in_tree+=ret_value;
162 if (dp->d_fileno != 0)
163 printf("%s\n", dp->d_name);*/
168 ERROR("getdirentries");
173 return files_in_tree;
176 static int queue_len_read (void)
178 char path[MAX_PATH_LEN];
180 // struct dirent *root_entry, *node_entry;
181 int messages_in_queue, messages_todo;
185 snprintf(path,MAX_PATH_LEN,"%s/queue/mess/",qmail_base_dir);
186 //WARNING("PATH TO READ: %s\n",path);
187 path_len=strlen(path);
188 if (path[path_len]!='/') {
190 path_len=strlen(path);
193 messages_in_queue=count_files_in_tree(path);
195 snprintf(path,MAX_PATH_LEN,"%s/queue/todo/",qmail_base_dir);
196 //WARNING("PATH TO READ: %s\n",path);
197 messages_todo=count_files_in_tree(path);
198 if ( (messages_todo!=-1) && (messages_in_queue!=-1) ) {
199 qmail_queue_submit(messages_in_queue,messages_todo);
205 static int qmail_config (const char *key, const char *val)
207 if (strcasecmp ("QmailDir", key) == 0)
209 free(qmail_base_dir);
210 qmail_base_dir=strdup(val);
211 WARNING("Setting Qmail base dir to %s\n", qmail_base_dir);
215 void module_register (void)
217 qmail_base_dir=strdup("/var/qmail");
218 plugin_register_config ("qmail_queue", qmail_config,
219 config_keys, config_keys_num);
221 plugin_register_read ("qmail_queue", queue_len_read);
222 } /* void module_register */