]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/smux.cpp
New sensor added: ActiveUsers
[stg.git] / projects / stargazer / plugins / other / smux / smux.cpp
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <arpa/inet.h>
4
5 #include <cstring>
6 #include <cerrno>
7 #include <ctime>
8 #include <csignal>
9 #include <cassert>
10
11 #include <vector>
12 #include <algorithm>
13 #include <iterator>
14 #include <stdexcept>
15 #include <utility>
16
17 #include "stg/common.h"
18 #include "stg/plugin_creator.h"
19
20 #include "smux.h"
21 #include "utils.h"
22
23 PLUGIN_CREATOR<SMUX> smc;
24
25 PLUGIN * GetPlugin()
26 {
27 return smc.GetPlugin();
28 }
29
30 bool SPrefixLess(const Sensors::value_type & a,
31                  const Sensors::value_type & b)
32 {
33 return a.first.PrefixLess(b.first);
34 }
35
36 SMUX_SETTINGS::SMUX_SETTINGS()
37     : errorStr(),
38       ip(0),
39       port(0),
40       password()
41 {}
42
43 int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
44 {
45 PARAM_VALUE pv;
46 std::vector<PARAM_VALUE>::const_iterator pvi;
47 int p;
48
49 pv.param = "Port";
50 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
51 if (pvi == s.moduleParams.end())
52     {
53     errorStr = "Parameter \'Port\' not found.";
54     printfd(__FILE__, "Parameter 'Port' not found\n");
55     return -1;
56     }
57 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
58     {
59     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
60     printfd(__FILE__, "Cannot parse parameter 'Port'\n");
61     return -1;
62     }
63 port = p;
64
65 pv.param = "Password";
66 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
67 if (pvi == s.moduleParams.end())
68     {
69     errorStr = "Parameter \'Password\' not found.";
70     printfd(__FILE__, "Parameter 'Password' not found\n");
71     password = "";
72     }
73 else
74     {
75     password = pvi->value[0];
76     }
77
78 pv.param = "Server";
79 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
80 if (pvi == s.moduleParams.end())
81     {
82     errorStr = "Parameter \'Server\' not found.";
83     printfd(__FILE__, "Parameter 'Server' not found\n");
84     return -1;
85     }
86 ip = inet_strington(pvi->value[0]);
87
88 return 0;
89 }
90
91 SMUX::SMUX()
92     : PLUGIN(),
93       users(NULL),
94       tariffs(NULL),
95       admins(NULL),
96       services(NULL),
97       corporations(NULL),
98       traffcounter(NULL),
99       errorStr(),
100       smuxSettings(),
101       settings(),
102       thread(),
103       mutex(),
104       running(false),
105       stopped(true),
106       sock(-1),
107       smuxHandlers(),
108       pdusHandlers(),
109       sensors(),
110       tables(),
111       notifiers(),
112       addUserNotifier(*this),
113       delUserNotifier(*this),
114       addDelTariffNotifier(*this)
115 {
116 pthread_mutex_init(&mutex, NULL);
117
118 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
119 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
120 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
121 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
122
123 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
124 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
125 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
126 }
127
128 SMUX::~SMUX()
129 {
130     {
131     Sensors::iterator it;
132     for (it = sensors.begin(); it != sensors.end(); ++it)
133         delete it->second;
134     }
135     {
136     Tables::iterator it;
137     for (it = tables.begin(); it != tables.end(); ++it)
138         delete it->second;
139     }
140 printfd(__FILE__, "SMUX::~SMUX()\n");
141 pthread_mutex_destroy(&mutex);
142 }
143
144 int SMUX::ParseSettings()
145 {
146 return smuxSettings.ParseSettings(settings);
147 }
148
149 int SMUX::Start()
150 {
151 assert(users != NULL && "users must not be NULL");
152 assert(tariffs != NULL && "tariffs must not be NULL");
153 assert(admins != NULL && "admins must not be NULL");
154 assert(services != NULL && "services must not be NULL");
155 assert(corporations != NULL && "corporations must not be NULL");
156 assert(traffcounter != NULL && "traffcounter must not be NULL");
157
158 if (PrepareNet())
159     return -1;
160
161 // Users
162 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
163 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
164 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
165 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
166 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
167 sensors[OID(".1.3.6.1.4.1.38313.1.1.6")] = new DisabledDetailStatsUsersSensor(*users);
168 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledUsersSensor(*users);
169 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new PassiveUsersSensor(*users);
170 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new CreditUsersSensor(*users);
171 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new FreeMbUsersSensor(*users);
172 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new TariffChangeUsersSensor(*users);
173 sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new ActiveUsersSensor(*users);
174 // Tariffs
175 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
176 // Admins
177 sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins);
178 // Services
179 sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services);
180 // Corporations
181 sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations);
182 // Traffcounter
183 sensors[OID(".1.3.6.1.4.1.38313.1.6.1")] = new TotalRulesSensor(*traffcounter);
184
185 // Table data
186 tables[".1.3.6.1.4.1.38313.1.2.2"] = new TariffUsersTable(".1.3.6.1.4.1.38313.1.2.2", *tariffs, *users);
187
188 UpdateTables();
189 SetNotifiers();
190
191 #ifdef SMUX_DEBUG
192 Sensors::const_iterator it(sensors.begin());
193 while (it != sensors.end())
194     {
195     printfd(__FILE__, "%s = %s\n",
196             it->first.ToString().c_str(),
197             it->second->ToString().c_str());
198     ++it;
199     }
200 #endif
201
202 if (!running)
203     {
204     if (pthread_create(&thread, NULL, Runner, this))
205         {
206         errorStr = "Cannot create thread.";
207         printfd(__FILE__, "Cannot create thread\n");
208         return -1;
209         }
210     }
211
212 return 0;
213 }
214
215 int SMUX::Stop()
216 {
217 printfd(__FILE__, "SMUX::Stop() - Before\n");
218 running = false;
219
220 if (!stopped)
221     {
222     //5 seconds to thread stops itself
223     for (int i = 0; i < 25 && !stopped; i++)
224         {
225         struct timespec ts = {0, 200000000};
226         nanosleep(&ts, NULL);
227         }
228     }
229
230 if (stopped)
231     pthread_join(thread, NULL);
232
233 ResetNotifiers();
234
235     {
236     Tables::iterator it;
237     for (it = tables.begin(); it != tables.end(); ++it)
238         delete it->second;
239     }
240     {
241     Sensors::iterator it;
242     for (it = sensors.begin(); it != sensors.end(); ++it)
243         delete it->second;
244     }
245
246 tables.erase(tables.begin(), tables.end());
247 sensors.erase(sensors.begin(), sensors.end());
248
249 close(sock);
250
251 if (!stopped)
252     {
253     running = true;
254     return -1;
255     }
256
257 printfd(__FILE__, "SMUX::Stop() - After\n");
258 return 0;
259 }
260
261 int SMUX::Reload()
262 {
263 if (Stop())
264     return -1;
265 if (Start())
266     return -1;
267 return 0;
268 }
269
270 void * SMUX::Runner(void * d)
271 {
272 sigset_t signalSet;
273 sigfillset(&signalSet);
274 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
275
276 SMUX * smux = static_cast<SMUX *>(d);
277
278 smux->Run();
279
280 return NULL;
281 }
282
283 void SMUX::Run()
284 {
285 stopped = true;
286 if (!SendOpenPDU(sock))
287     return;
288 if (!SendRReqPDU(sock))
289     return;
290 running = true;
291 stopped = false;
292
293 while(running)
294     {
295     if (WaitPackets(sock))
296         {
297         SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
298         if (pdus)
299             {
300             DispatchPDUs(pdus);
301             ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
302             }
303         }
304     if (!running)
305         break;
306     }
307 SendClosePDU(sock);
308 stopped = true;
309 }
310
311 bool SMUX::PrepareNet()
312 {
313 sock = socket(AF_INET, SOCK_STREAM, 0);
314
315 if (sock < 0)
316     {
317     errorStr = "Cannot create socket.";
318     printfd(__FILE__, "Cannot create socket\n");
319     return true;
320     }
321
322 struct sockaddr_in addr;
323
324 addr.sin_family = AF_INET;
325 addr.sin_port = htons(smuxSettings.GetPort());
326 addr.sin_addr.s_addr = smuxSettings.GetIP();
327
328 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
329     {
330     errorStr = "Cannot connect.";
331     printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
332     return true;
333     }
334
335 return false;
336 }
337
338 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
339 {
340 SMUXHandlers::iterator it;
341 it = smuxHandlers.find(pdus->present);
342 if (it != smuxHandlers.end())
343     {
344     return (this->*(it->second))(pdus);
345     }
346 else
347     {
348 #ifdef SMUX_DEBUG
349     switch (pdus->present)
350         {
351         case SMUX_PDUs_PR_NOTHING:
352             printfd(__FILE__, "PDUs: nothing\n");
353             break;
354         case SMUX_PDUs_PR_open:
355             printfd(__FILE__, "PDUs: open\n");
356             break;
357         case SMUX_PDUs_PR_registerRequest:
358             printfd(__FILE__, "PDUs: registerRequest\n");
359             break;
360         default:
361             printfd(__FILE__, "PDUs: undefined\n");
362         }
363     asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
364 #endif
365     }
366 return false;
367 }
368
369 bool SMUX::UpdateTables()
370 {
371 Sensors newSensors;
372 bool done = true;
373 Tables::iterator it(tables.begin());
374 while (it != tables.end())
375     {
376     try
377         {
378         it->second->UpdateSensors(newSensors);
379         }
380     catch (const std::runtime_error & ex)
381         {
382         printfd(__FILE__,
383                 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
384                 it->first.c_str(), ex.what());
385         done = false;
386         break;
387         }
388     ++it;
389     }
390 if (!done)
391     {
392     Sensors::iterator it(newSensors.begin());
393     while (it != newSensors.end())
394         {
395         delete it->second;
396         ++it;
397         }
398     return false;
399     }
400
401 it = tables.begin();
402 while (it != tables.end())
403     {
404     std::pair<Sensors::iterator, Sensors::iterator> res;
405     res = std::equal_range(sensors.begin(),
406                            sensors.end(),
407                            std::pair<OID, Sensor *>(OID(it->first), NULL),
408                            SPrefixLess);
409     Sensors::iterator sit(res.first);
410     while (sit != res.second)
411         {
412         delete sit->second;
413         ++sit;
414         }
415     sensors.erase(res.first, res.second);
416     ++it;
417     }
418
419 sensors.insert(newSensors.begin(), newSensors.end());
420
421 return true;
422 }
423
424 void SMUX::SetNotifier(USER_PTR userPtr)
425 {
426 notifiers.push_back(CHG_AFTER_NOTIFIER(*this, userPtr));
427 userPtr->GetProperty().tariffName.AddAfterNotifier(&notifiers.back());
428 }
429
430 void SMUX::UnsetNotifier(USER_PTR userPtr)
431 {
432 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
433 while (it != notifiers.end())
434     {
435     if (it->GetUserPtr() == userPtr)
436         {
437         userPtr->GetProperty().tariffName.DelAfterNotifier(&(*it));
438         notifiers.erase(it);
439         break;
440         }
441     ++it;
442     }
443 }
444
445 void SMUX::SetNotifiers()
446 {
447 int h = users->OpenSearch();
448 assert(h && "USERS::OpenSearch is always correct");
449
450 USER_PTR u;
451 while (!users->SearchNext(h, &u))
452     SetNotifier(u);
453
454 users->CloseSearch(h);
455
456 users->AddNotifierUserAdd(&addUserNotifier);
457 users->AddNotifierUserDel(&delUserNotifier);
458
459 tariffs->AddNotifierAdd(&addDelTariffNotifier);
460 tariffs->AddNotifierDel(&addDelTariffNotifier);
461 }
462
463 void SMUX::ResetNotifiers()
464 {
465 tariffs->DelNotifierDel(&addDelTariffNotifier);
466 tariffs->DelNotifierAdd(&addDelTariffNotifier);
467
468 users->DelNotifierUserDel(&delUserNotifier);
469 users->DelNotifierUserAdd(&addUserNotifier);
470
471 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
472 while (it != notifiers.end())
473     {
474     it->GetUserPtr()->GetProperty().tariffName.DelAfterNotifier(&(*it));
475     ++it;
476     }
477 }
478
479 void CHG_AFTER_NOTIFIER::Notify(const std::string &, const std::string &)
480 {
481 smux.UpdateTables();
482 }