970dda294e51326fd141e8d003154adacc0084b1
[lxde/libsysstat.git] / netstat.cpp
1 /* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 **
4 ** SysStat is a Qt-based interface to system statistics
5 **
6 ** Authors:
7 ** Copyright (c) 2009 - 2012 Kuzma Shapran <Kuzma.Shapran@gmail.com>
8 **
9 ** This library is free software; you can redistribute it and/or
10 ** modify it under the terms of the GNU Lesser General Public
11 ** License as published by the Free Software Foundation; either
12 ** version 2.1 of the License, or (at your option) any later version.
13 **
14 ** This library is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ** Lesser General Public License for more details.
18 **
19 ** You should have received a copy of the GNU Lesser General Public
20 ** License along with this library;
21 ** if not, write to the Free Software Foundation, Inc.,
22 ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 **
24 ** END_COMMON_COPYRIGHT_HEADER */
25
26
27 #include "netstat.h"
28 #include "netstat_p.h"
29
30
31 namespace SysStat {
32
33 NetStatPrivate::NetStatPrivate(NetStat *parent)
34 : BaseStatPrivate(parent)
35 {
36 mSource = defaultSource();
37
38 connect(mTimer, SIGNAL(timeout()), SLOT(timeout()));
39
40
41 QStringList rows(readAllFile("/proc/net/dev").split(QChar('\n'), QString::SkipEmptyParts));
42
43 rows.erase(rows.begin(), rows.begin() + 2);
44
45 foreach (const QString &row, rows)
46 {
47 QStringList tokens = row.split(QChar(':'), QString::SkipEmptyParts);
48 if (tokens.size() != 2)
49 continue;
50
51 mSources.append(tokens[0].trimmed());
52 }
53 }
54
55 NetStatPrivate::~NetStatPrivate()
56 {
57 }
58
59 void NetStatPrivate::timeout()
60 {
61 QStringList rows(readAllFile("/proc/net/dev").split(QChar('\n'), QString::SkipEmptyParts));
62
63
64 if (rows.size() < 2)
65 return;
66
67 QStringList names = rows[1].split(QChar('|'));
68 if (names.size() != 3)
69 return;
70 QStringList namesR = names[1].split(QChar(' '), QString::SkipEmptyParts);
71 QStringList namesT = names[2].split(QChar(' '), QString::SkipEmptyParts);
72 int receivedIndex = namesR.indexOf("bytes");
73 int transmittedIndex = namesR.size() + namesT.indexOf("bytes");
74
75 rows.erase(rows.begin(), rows.begin() + 2);
76
77 foreach (const QString &row, rows)
78 {
79 QStringList tokens = row.split(QChar(':'), QString::SkipEmptyParts);
80 if (tokens.size() != 2)
81 continue;
82
83 QString interfaceName = tokens[0].trimmed();
84
85 QStringList data = tokens[1].split(QChar(' '), QString::SkipEmptyParts);
86 if (data.size() < transmittedIndex)
87 continue;
88
89 Values current;
90 current.received = data[receivedIndex ].toULongLong();
91 current.transmitted = data[transmittedIndex].toULongLong();
92
93 if (!mPrevious.contains(interfaceName))
94 mPrevious.insert(interfaceName, Values());
95 const Values &previous = mPrevious[interfaceName];
96
97 if (interfaceName == mSource)
98 emit update((( current.received - previous.received ) * 1000 ) / mTimer->interval(), (( current.transmitted - previous.transmitted ) * 1000 ) / mTimer->interval());
99
100 mPrevious[interfaceName] = current;
101 }
102 }
103
104 QString NetStatPrivate::defaultSource()
105 {
106 return "lo";
107 }
108
109 NetStatPrivate::Values::Values()
110 : received(0)
111 , transmitted(0)
112 {
113 }
114
115 NetStat::NetStat(QObject *parent)
116 : BaseStat(parent)
117 {
118 impl = new NetStatPrivate(this);
119 baseimpl = impl;
120
121 connect(impl, SIGNAL(update(unsigned,unsigned)), this, SIGNAL(update(unsigned,unsigned)));
122 }
123
124 NetStat::~NetStat()
125 {
126 }
127
128 }