summaryrefslogtreecommitdiff
path: root/src/devreader-linux-proc.cpp
blob: 2d698b7ad2ce00722e57b081a389002ab33eddae (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
/***************************************************************************
 *                                                                         *
 *   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.                                   * 
 *                                                                         *
 ***************************************************************************/

#include "devreader-linux-proc.h"
#include "stringutils.h"

#include <fstream>
#include <list>
#include <string>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

using namespace std;

DevReaderLinuxProc::DevReaderLinuxProc(const string& deviceName)
    : DevReader(deviceName)
{
}

DevReaderLinuxProc::~DevReaderLinuxProc()
{
}

bool DevReaderLinuxProc::isAvailable()
{
    struct stat procStat;
    if(stat("/proc/net/dev", &procStat) < 0 || ! S_ISREG(procStat.st_mode))
        return false;

    return true;
}

list<string> DevReaderLinuxProc::findAllDevices()
{
    list<string> interfaceNames;
    
    ifstream fin("/proc/net/dev");
    if(!fin.is_open())
        return interfaceNames;
    
    // skip the two header lines
    string line;
    getline(fin, line);
    getline(fin, line);

    if(!fin.good())
        return interfaceNames;

    // read all remaining lines and extract the device name
    while(fin.good())
    {
        getline(fin, line);
        if(line.empty())
            continue;

        string::size_type posEnd = line.find(':');
        if(posEnd == string::npos)
            continue;

        interfaceNames.push_back(trim(line.substr(0, posEnd)));
    }
    
    return interfaceNames;
}

void DevReaderLinuxProc::readFromDevice(DataFrame& dataFrame)
{
    if(m_deviceName.empty())
        return;
    
    ifstream fin("/proc/net/dev");
    if(!fin.is_open())
        return;
    
    // skip the two header lines
    string line;
    getline(fin, line);
    getline(fin, line);

    if(!fin.good())
        return;

    // search for device
    while(fin.good())
    {
        getline(fin, line);
        if(line.empty())
            continue;

        string::size_type posEnd = line.find(':');
        if(posEnd == string::npos)
            continue;

        string interfaceName = trim(line.substr(0, posEnd));
        if(interfaceName.empty())
            continue;

        // check if it is the device we want
        if(m_deviceName != interfaceName)
            continue;

        // read device data
        unsigned long long bytesIn = 0;
        unsigned long long packetsIn = 0;
        unsigned long long errorsIn = 0;
        unsigned long long dropsIn = 0;
        unsigned long long bytesOut = 0;
        unsigned long long packetsOut = 0;
        unsigned long long errorsOut = 0;
        unsigned long long dropsOut = 0;
        unsigned long long dummy = 0;

        istringstream sin(trim(line.substr(posEnd + 1)));

        sin >> bytesIn
            >> packetsIn
            >> errorsIn
            >> dropsIn
            >> dummy
            >> dummy
            >> dummy
            >> dummy
            >> bytesOut
            >> packetsOut
            >> errorsOut
            >> dropsOut;

        if(sin.fail())
            break;
        
        dataFrame.setTotalDataIn(bytesIn);
        dataFrame.setTotalDataOut(bytesOut);

        dataFrame.setTotalPacketsIn(packetsIn);
        dataFrame.setTotalPacketsOut(packetsOut);

        dataFrame.setTotalErrorsIn(errorsIn);
        dataFrame.setTotalErrorsOut(errorsOut);
        
        dataFrame.setTotalDropsIn(dropsIn);
        dataFrame.setTotalDropsOut(dropsOut);
        
        dataFrame.setValid(true);
        
        break;
    }
}