]> jfr.im git - irc/quakenet/newserv.git/blame - graphing/graphing.c
Fix various warnings.
[irc/quakenet/newserv.git] / graphing / graphing.c
CommitLineData
a6755701
CP
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"
a6755701
CP
8#include "../core/schedule.h"
9#include "../core/error.h"
10#include "../lib/sha1.h"
11#include "../lib/hmac.h"
12
8186335d 13#include "graphing.h"
a6755701
CP
14
15static void gr_newserver(int hook, void *arg);
16static void gr_lostserver(int hook, void *arg);
17static void tick(void *arg);
18static void openserver(int servernum);
19static void closeserver(int servernum);
a6755701 20
8186335d 21fsample_m *servergraphs[MAXSERVERS];
a6755701
CP
22static void *sched;
23
24static sstring *path;
25
26void _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
46void _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
8186335d 61int servermonitored(int servernum) {
a6755701
CP
62 return servergraphs[servernum] != NULL;
63}
64
65static 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
73static 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;
8186335d 78 fsample_m *m;
a6755701
CP
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");
8186335d
CP
93 if(!f) {
94 Error("graphing", ERR_WARNING, "Unable to create name file for %s (%s.name)", serverlist[servernum].name->content, filename);
a6755701 95 return;
8186335d 96 }
a6755701
CP
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
45eadff3 109 m = fsopen_m(GRAPHING_DATASETS, appendsuffix(filename, ".0"), SAMPLES, (CoreHandlerAddFn)registercorehandler, (CoreHandlerDelFn)deregistercorehandler);
8186335d
CP
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;
a6755701
CP
127}
128
129static void closeserver(int servernum) {
130 if(!servermonitored(servernum))
131 return;
132
133 fsclose_m(servergraphs[servernum]);
134 servergraphs[servernum] = NULL;
135}
136
137static void gr_newserver(int hook, void *arg) {
138 long num = (long)arg;
139
140 openserver(num);
141}
142
143static void gr_lostserver(int hook, void *arg) {
144 long num = (long)arg;
145
146 closeserver(num);
147}
148
149static void tick(void *arg) {
150 time_t t = time(NULL);
151 int i;
152
8186335d 153 if(t % GRAPHING_RESOLUTION != 0)
a6755701
CP
154 return;
155
156 for(i=0;i<MAXSERVERS;i++)
157 if(servermonitored(i))
8186335d 158 fsset_m(servergraphs[i], t / GRAPHING_RESOLUTION, servercount[i]);
a6755701
CP
159}
160
161