-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqueue.c
124 lines (101 loc) · 2.44 KB
/
queue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include <dirent.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "callbacks.h"
#include "err.h"
#include "fs.h"
#include "netdata.h"
#include "queue.h"
#define QMAIL_QUEUE_PATH "/var/qmail/queue/"
struct queue_statistics {
int mess;
int todo;
};
static
void *
queue_data_init() {
struct queue_statistics * ret;
if (is_directory(QMAIL_QUEUE_PATH "mess") != 1) {
return NULL;
}
if (is_directory(QMAIL_QUEUE_PATH "todo") != 1) {
return NULL;
}
ret = calloc(1, sizeof * ret);
return ret;
}
static
int
print_queue_hdr(const char * name) {
nd_chart("qmail", "queue", NULL, NULL, NULL, NULL, NULL, "qmail.queue", ND_CHART_TYPE_AREA);
nd_dimension("mess", NULL, ND_ALG_ABSOLUTE, 1, 1, ND_VISIBLE);
nd_dimension("todo", NULL, ND_ALG_ABSOLUTE, 1, 1, ND_VISIBLE);
return fflush(stdout);
}
static
int
print_queue_data(const char * name, const struct queue_statistics * data, const unsigned long time) {
nd_begin_time("qmail", "queue", NULL, time);
nd_set("mess", data->mess);
nd_set("todo", data->todo);
nd_end();
return fflush(stdout);
}
static
int
measure_dir(const char * name) {
char path[PATH_MAX];
struct dirent * de;
int res = 0;
DIR * dir;
dir = opendir(name);
if (dir == NULL) {
fprintf(stderr, "Cannot open dir: %s\n", name);
return 0;
}
while ((de = readdir(dir))) {
if (de->d_name[0] == '.')
continue;
if (de->d_type == DT_DIR) {
sprintf(path, "%s/%s", name, de->d_name);
res += measure_dir(path);
} else if (de->d_type == DT_REG) {
res += 1;
} else if (de->d_type == DT_UNKNOWN) {
sprintf(path, "%s/%s", name, de->d_name);
if (is_directory(path)) {
res += measure_dir(path);
} else {
res += 1;
}
}
}
closedir(dir);
return res;
}
static
void
measure_queue(const char * unused, struct queue_statistics * data) {
data->mess = measure_dir(QMAIL_QUEUE_PATH "mess");
data->todo = measure_dir(QMAIL_QUEUE_PATH "todo");
}
static
void
clear_data(struct queue_statistics * data) {
memset(data, 0, sizeof * data);
}
static
struct stat_func queue = {
.init = &queue_data_init,
.fini = &free,
.print_hdr = &print_queue_hdr,
.print = (int (*)(const char *, const void *, unsigned long))&print_queue_data,
.process = (void (*)(const char *, void *))&measure_queue,
.postprocess = NULL,
.clear = (void (*)(void *))&clear_data,
};
struct stat_func * queue_func = &queue;