File: measureserver.cc

package info (click to toggle)
sinfo 0.0.48-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,332 kB
  • sloc: sh: 11,213; cpp: 6,722; makefile: 271; xml: 151; perl: 149
file content (240 lines) | stat: -rw-r--r-- 5,988 bytes parent folder | download | duplicates (3)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <boost/bind.hpp>
#include <iostream>
#include "messagebasictypes.h"
#include "messagecomplextypes.h"
#include "messagesinfotypes.h"
#include "messagestlcontainertypes.h"
#include "measureserver.h"
#include "meminfometer.h"
#include "loadavgmeter.h"
#include "cpuinfometer.h"
#include "unamemeter.h"
#include "uptimemeter.h"
#include "usersmeter.h"
#include "sinfobroadcastids.h"
using namespace std;
using namespace Msg;


// constants for the broadcasting timing
////////////////////////////////////////


// if sinfo produces to much net load for your purposes,
// you may twiddle with the following factors.


// don't send informations to sinfo if the computer was last heard
// for the last 25 seconds
// FIXME ? implement this again?
// const unsigned long dropoldTimer = 25;


// check the cpu-load every second....
// if the load has not changed (less 3%),
// broadcast every 10 seconds otherwise broadcast every second.
// if the CPU load has changed much (groeter 30%) trigger the
// sending of the process informations.
const unsigned long cpustatTimer = 1;
const unsigned long cpustatTimerMax = 10;
const float cpustatTimerFastThr = 0.05;      // fastBcast threshold in percentage of idle time
const float cpustatTimerProcinfoThr = 0.3;   // sendProcinfo threshold in percentage of idle time

const unsigned long loadavgTimer = 10;
const unsigned long uptimeTimer = 10;
const unsigned long procinfoTimer = 10;
const unsigned long netloadTimer = 10;
const unsigned long diskloadTimer = 10;
const unsigned long meminfoTimer = 30;
const unsigned long usersTimer = 30;
const unsigned long unamestrTimer = 900;
const unsigned long cpuinfoTimer = 60;
const unsigned long markerTimer = 900;

// if we received a restart flag we will send out ALL informations
// several times with a broadcasting intervall of 10 seconds.
const unsigned long restartTimer = 10;


MeasureServer::MeasureServer(boost::asio::io_service& io_service, const std::string & networkcard, const std::string & _marker, bool cmdlinemode, std::list < std::string > ignoreList, long listtop):ioservice(io_service), timer(io_service), procinfoMeter(cmdlinemode,ignoreList), listtop(listtop),  netloadMeter(networkcard), marker(_marker)
{
  timeCounter = 0;
  procinfonow = false;
  sendRestartFlag = true;
  lastidlepercent = 0.;

  timer.expires_from_now(boost::posix_time::milliseconds(500));
  timer.async_wait(boost::bind(&MeasureServer::handle_timeout, this));

  restartcounter=0;
}


MeasureServer::~MeasureServer()
{
}


void MeasureServer::timerEvent()
{
  Message message;

  if (0 == (timeCounter%meminfoTimer))
  {
    Meminfo meminfo;
    if (getMeminfo(meminfo))
    {
      pushFrontMeminfo(message, meminfo);
      pushFrontuint8(message, meminfoFlag);
    }
  }

  if (0 == (timeCounter%loadavgTimer))
  {
    Loadavg loadavg;
    if (getLoadavg(loadavg))
    {
      pushFrontLoadavg(message, loadavg);
      pushFrontuint8(message, loadavgFlag);
    }
  }

  if (0 == (timeCounter%cpustatTimer))
  {
    Cpustat cpustat;
    if (cpustatMeter.getCpustat(cpustat))
    {
      if ((0 == timeCounter)
          || (fabs(lastidlepercent - cpustat.idlep) >= cpustatTimerFastThr)
          || (0==(timeCounter%cpustatTimerMax)) )
      {

        if (fabs(lastidlepercent - cpustat.idlep) >= cpustatTimerProcinfoThr)
          procinfonow = true;

        pushFrontCpustat(message, cpustat);
        pushFrontuint8(message, cpustatFlag);

        lastidlepercent = cpustat.idlep;
      }
    }
  }

  if (0 == (timeCounter%cpuinfoTimer))
  {
    Cpuinfo cpuinfo;
    if (getCpuinfo(cpuinfo))
    {
      pushFrontCpuinfo(message, cpuinfo);
      pushFrontuint8(message, cpuinfoFlag);
    }
  }
  if (0 == (timeCounter%markerTimer))
  {
    pushFrontstring(message, marker);
    pushFrontuint8(message, markerFlag);
  }

  if (0 == (timeCounter%netloadTimer))
  {
    Netload netload;
    if (netloadMeter.getNetload(netload))
    {
      pushFrontNetload(message, netload);
      pushFrontuint8(message, netloadFlag);
    }
  }

  if (0 == (timeCounter%diskloadTimer))
  {
    Diskload diskload;
    if (diskloadMeter.getDiskload(diskload))
    {
      pushFrontDiskload(message, diskload);
      pushFrontuint8(message, diskloadFlag);
    }
  }

  if (0 == (timeCounter%unamestrTimer))
  {
    Unameinfo unameinfo = getUnameinfo();

    pushFrontUnameinfo(message, unameinfo);
    pushFrontuint8(message, unameinfoFlag);
  }

  if (0 == (timeCounter%uptimeTimer))
  {
    Uptime uptime;
    if (getUptime(uptime))
    {
      pushFrontUptime(message, uptime);
      pushFrontuint8(message, uptimeFlag);
    }
  }

  if (0 == (timeCounter%usersTimer))
  {
    Users users = getUsers();

    pushFrontUsers(message, users);
    pushFrontuint8(message, usersFlag);
  }

  if ( (0 == (timeCounter%procinfoTimer)) || (procinfonow) )
  {
    ProcinfoList procinfoList;
    if (procinfoMeter.getTopList(listtop, procinfoList))
    {
      pushFront(message, procinfoList);
      pushFrontuint8(message, procinfoFlag);
    }
    procinfonow = false;
  }

  if (sendRestartFlag)
  {
    pushFrontuint8(message, restartFlag);
    sendRestartFlag = false;
  }

  if (message.size()>0)
  {
    // try-catch to avoid stop of program when udp port is not available due to
    // reconfiguration (e.g. ipatbles)
    try
    {
      cout << "MeasureServer::timerEvent sending " << message.size() << "bytes" << endl;
      sendMeasurementSignal(message);
    }
    catch (std::exception& e)
    {
      cerr << "Exception: " << e.what() << endl;
    }
  }

  timeCounter++;

  if ( (restartcounter>0) &&  (0 == (timeCounter%restartTimer)) )
  {
    timeCounter = 0;
    restartcounter--;
  }
}


void MeasureServer::restartCounterEvent()
{
  restartcounter=3;
}


void MeasureServer::handle_timeout()
{
  cout << "MeasureServer::handle_timeout" << endl;
  timerEvent();

  timer.expires_from_now(boost::posix_time::milliseconds(1000));
  timer.async_wait(boost::bind(&MeasureServer::handle_timeout, this));
}