summaryrefslogtreecommitdiffstats
path: root/superkaramba/src/networksensor.cpp
blob: 10138b4da884942ec4fbfe18ca35c73ef3c30554 (plain)
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
/***************************************************************************
 *   Copyright (C) 2003 by Hans Karlsson                                   *
 *   karlsson.h@home.se                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
#ifdef __FreeBSD__
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/socket.h>
#include <net/route.h>
#endif

#include "networksensor.h"

NetworkSensor::NetworkSensor( TQString dev, int interval ):Sensor( interval )
{
   device = dev.lower();

#ifdef __FreeBSD__
   /* Determine number of interfaces */
   u_int   n    = 0;
   size_t  nlen = 0;
   nlen = sizeof(n);
   sysctlbyname("net.link.generic.system.ifcount", &n, &nlen, NULL, 0);

   size_t  if_miblen = 0;
   if_miblen = sizeof(if_mib);
   static  int name[] = { CTL_NET,
                          PF_LINK,
                          NETLINK_GENERIC,
                          IFMIB_IFDATA,
                          0,
                          IFDATA_GENERAL };

   /*
      If the device was defined by the theme, find the right devicenumber.
      If not, use the device that holds the default route.
    */

   if_number = -1;
   int if_gw = -1;

   for (int i = 1; i <= n; ++i) {
      name[4] = i;
      /* Get data for iface-number i */
      sysctl(name, 6, (void*)&if_mib, (size_t*)&if_miblen, (void*)NULL, (size_t)0);

      /* We found the right interface? */
      if (TQString(if_mib.ifmd_name) == device) {
         if_number = i;
         break;
      }

      /* Does the interface hold the default route? */
      if ( if_mib.ifmd_flags & RTF_GATEWAY )
         if_gw = i;
   }

   if ((if_number == -1) && (if_gw != -1))
      if_number = if_gw;
#else
   if( device.isEmpty() )
    device = "eth0";
#endif
     getInOutBytes(receivedBytes,transmittedBytes);
    netTimer.start();

}
NetworkSensor::~NetworkSensor()
{
}
void NetworkSensor::getInOutBytes ( unsigned long &in,unsigned long &out) const
{
#ifdef __FreeBSD__
    if (if_number != -1) {
       size_t  if_miblen = 0;
       if_miblen = sizeof(if_mib);
       int name[] = { CTL_NET,
                      PF_LINK,
                      NETLINK_GENERIC,
                      IFMIB_IFDATA,
                      if_number,
                      IFDATA_GENERAL };

       sysctl(name, 6, (void*)&if_mib, (size_t*)&if_miblen, (void*)NULL, (size_t)0);

       in = if_mib.ifmd_data.ifi_ibytes;
       out = if_mib.ifmd_data.ifi_obytes;
   }
   else {
      in = 0;
      out = 0;
   }
#else
    TQFile file("/proc/net/dev");
    TQString line;
    if ( file.open(IO_ReadOnly | IO_Translate) )
    {
        TQTextStream t( &file );        // use a text stream
        line = t.readLine();
        while(line !=0 && !line.tqcontains(device))
        {
            line = t.readLine();
        }
        if ( line.tqcontains( device ) )
        {
            TQRegExp rx( "\\W+"+device+":\\D*(\\d+)(?:\\D+\\d+){7}\\D+(\\d+)", false);
            rx.search(line);
            in = rx.cap(1).toULong();
            out = rx.cap(2).toULong();
       }
        else
        {
            qDebug("Network sensor: can not tqfind %s", device.ascii());
            in = 0;
            out = 0;
        }
    file.close();
    }
#endif
}

void NetworkSensor::update()
{
    SensorParams *sp;
    Meter *meter;
    TQObjectListIt it( *objList );
    TQString format;
    int decimals;

    unsigned long inB, outB;
    const double delay = (double) netTimer.elapsed(); // msec elapsed since last update
    getInOutBytes( inB, outB );
    netTimer.restart();

    while( it != 0 )
    {
        sp = (SensorParams*)(*it);
        meter = sp->getMeter();
        format = sp->getParam( "FORMAT" );
        decimals = ( sp->getParam( "DECIMALS" ) ).toInt();
        if (format.length() == 0 )
        {
            format = "%in";
        }

        format.tqreplace( TQRegExp("%inkb", false), TQString::number( ((inB - receivedBytes)*8)/delay, 'f', decimals ) );
        format.tqreplace( TQRegExp("%in", false), TQString::number( (inB - receivedBytes)/delay, 'f', decimals ) );

        format.tqreplace( TQRegExp("%outkb", false), TQString::number( ((outB - transmittedBytes)*8)/delay, 'f', decimals ) );
        format.tqreplace( TQRegExp("%out", false), TQString::number( (outB - transmittedBytes)/delay, 'f', decimals ) );

        meter->setValue( format );
        ++it;
    }
    receivedBytes = inB;
    transmittedBytes = outB;
}

#include "networksensor.moc"