]> jfr.im git - irc/quakenet/newserv.git/blob - graphing/graphing.c
Fix warning.
[irc/quakenet/newserv.git] / graphing / graphing.c
1 #include <string.h>
2 #include <stdio.h>
3
4 #include "../core/hooks.h"
5 #include "../core/config.h"
6 #include "../usercount/usercount.h"
7 #include "../lib/sstring.h"
8 #include "../core/schedule.h"
9 #include "../core/error.h"
10 #include "../lib/sha1.h"
11 #include "../lib/hmac.h"
12
13 #include "graphing.h"
14
15 static void gr_newserver(int hook, void *arg);
16 static void gr_lostserver(int hook, void *arg);
17 static void tick(void *arg);
18 static void openserver(int servernum);
19 static void closeserver(int servernum);
20
21 fsample_m *servergraphs[MAXSERVERS];
22 static void *sched;
23
24 static sstring *path;
25
26 void _init(void) {
27 int i;
28
29 memset(servergraphs, 0, sizeof(servergraphs));
30
31 path = getcopyconfigitem("graphing", "path", "", 100);
32 if(!path || !path->content || !path->content[0]) {
33 Error("graphing", ERR_WARNING, "Path not set, not loaded.");
34 return;
35 }
36
37 for(i=0;i<MAXSERVERS;i++)
38 openserver(i);
39
40 registerhook(HOOK_SERVER_NEWSERVER, gr_newserver);
41 registerhook(HOOK_SERVER_LOSTSERVER, gr_lostserver);
42
43 sched = schedulerecurring(time(NULL), 0, 1, tick, NULL);
44 }
45
46 void _fini(void) {
47 int i;
48 if(sched)
49 deleteschedule(sched, tick, NULL);
50
51 for(i=0;i<MAXSERVERS;i++)
52 if(servermonitored(i))
53 closeserver(i);
54
55 freesstring(path);
56
57 deregisterhook(HOOK_SERVER_NEWSERVER, gr_newserver);
58 deregisterhook(HOOK_SERVER_LOSTSERVER, gr_lostserver);
59 }
60
61 int servermonitored(int servernum) {
62 return servergraphs[servernum] != NULL;
63 }
64
65 static char *appendsuffix(char *prefix, char *suffix) {
66 static char buf[1024];
67
68 snprintf(buf, sizeof(buf), "%s%s", prefix, suffix);
69
70 return buf;
71 }
72
73 static void openserver(int servernum) {
74 unsigned char digest[SHA1_DIGESTSIZE];
75 char filename[512], hexdigest[sizeof(digest)*2 + 1];
76 FILE *f;
77 SHA1_CTX sha;
78 fsample_m *m;
79
80 if(servermonitored(servernum))
81 return;
82
83 if(serverlist[servernum].linkstate == LS_INVALID)
84 return;
85
86 SHA1Init(&sha);
87 SHA1Update(&sha, (unsigned char *)serverlist[servernum].name->content, serverlist[servernum].name->length);
88 SHA1Final(digest, &sha);
89
90 snprintf(filename, sizeof(filename), "%s/%s", path->content, hmac_printhex(digest, hexdigest, sizeof(digest)));
91
92 f = fopen(appendsuffix(filename, ".name"), "w");
93 if(!f) {
94 Error("graphing", ERR_WARNING, "Unable to create name file for %s (%s.name)", serverlist[servernum].name->content, filename);
95 return;
96 }
97
98 fprintf(f, "%s\n", serverlist[servernum].name->content);
99 fclose(f);
100
101 /* 0: seconds
102 1: minutes
103 2: hours
104 3: days
105 4: weeks
106 5: months
107 */
108
109 m = fsopen_m(GRAPHING_DATASETS, appendsuffix(filename, ".0"), SAMPLES, (CoreHandlerAddFn)registercorehandler, (CoreHandlerDelFn)deregistercorehandler);
110 if(!m) {
111 Error("graphing", ERR_WARNING, "Unable to create main backing store for %s (%s.0)", serverlist[servernum].name->content, filename);
112 return;
113 }
114 /*
115 if(!fsadd_m(m, appendsuffix(filename, ".1"), PERMINUTE, fsapmean, (void *)PERMINUTE) ||
116 !fsadd_m(m, appendsuffix(filename, ".2"), PERMINUTE * 24, fsapmean, (void *)(PERMINUTE * 24)) ||
117 !fsadd_m(m, appendsuffix(filename, ".3"), PERMINUTE * 24 * 7, fsapmean, (void *)(PERMINUTE * 24 * 7)) ||
118 !fsadd_m(m, appendsuffix(filename, ".4"), PERMINUTE * 24 * 7 * 4, fsapmean, (void *)(PERMINUTE * 24 * 7 * 4)) ||
119 !fsadd_m(m, appendsuffix(filename, ".5"), PERMINUTE * 24 * 7 * 4 * 12, fsapmean, (void *)(PERMINUTE * 24 * 7 * 4 * 12)))
120 {
121 Error("graphing", ERR_WARNING, "Unable to create main side store for %s (%s.X)", serverlist[servernum].name->content, filename);
122 fsclose_m(m);
123 return;
124 }
125 */
126 servergraphs[servernum] = m;
127 }
128
129 static void closeserver(int servernum) {
130 if(!servermonitored(servernum))
131 return;
132
133 fsclose_m(servergraphs[servernum]);
134 servergraphs[servernum] = NULL;
135 }
136
137 static void gr_newserver(int hook, void *arg) {
138 long num = (long)arg;
139
140 openserver(num);
141 }
142
143 static void gr_lostserver(int hook, void *arg) {
144 long num = (long)arg;
145
146 closeserver(num);
147 }
148
149 static void tick(void *arg) {
150 time_t t = time(NULL);
151 int i;
152
153 if(t % GRAPHING_RESOLUTION != 0)
154 return;
155
156 for(i=0;i<MAXSERVERS;i++)
157 if(servermonitored(i))
158 fsset_m(servergraphs[i], t / GRAPHING_RESOLUTION, servercount[i]);
159 }
160
161