13 #include <QTextStream>
14 #include <QNetworkInterface>
19 #include <sys/param.h>
20 #include <sys/sysctl.h>
21 #include <sys/socket.h>
22 #include <net/route.h>
27 device = dev.toLower();
34 sysctlbyname(
"net.link.generic.system.ifcount", &n, &nlen, NULL, 0);
37 if_miblen =
sizeof(if_mib);
55 for (
int i = 1; i <= n; ++i) {
58 sysctl(name, 6, (
void*)&if_mib, (
size_t*)&if_miblen, (
void*)NULL, (
size_t)0);
61 if (QString(if_mib.ifmd_name) == device) {
67 if (if_mib.ifmd_flags & RTF_GATEWAY)
71 if ((if_number == -1) && (if_gw != -1))
74 if (device.isEmpty()) {
78 interfaceList = device.split(
'|', QString::SkipEmptyParts);
82 getInOutBytes(receivedBytes, transmittedBytes);
87 void NetworkSensor::refreshDevice() {
88 int rank = interfaceList.count();
90 QFile file (
"/proc/net/dev");
92 if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
96 QString line = t.readLine();
98 QStringList::ConstIterator listEnd(interfaceList.constEnd());
99 while ((rank != 0) && (line != 0)) {
101 for (QStringList::ConstIterator dev = interfaceList.constBegin();
102 (dev != listEnd) && (i < rank);
105 if (line.contains(*dev)) {
116 if (rank >= interfaceList.count()) {
126 void NetworkSensor::getInOutBytes(
unsigned long &in,
unsigned long &out)
const
129 if (if_number != -1) {
130 size_t if_miblen = 0;
131 if_miblen =
sizeof(if_mib);
132 int name[] = { CTL_NET,
139 sysctl(name, 6, (
void*)&if_mib, (
size_t*)&if_miblen, (
void*)NULL, (
size_t)0);
141 in = if_mib.ifmd_data.ifi_ibytes;
142 out = if_mib.ifmd_data.ifi_obytes;
148 QFile file(
"/proc/net/dev");
150 if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
151 QTextStream t(&file);
154 while (line != 0 && !line.contains(device)) {
158 if (line.contains(device)) {
159 QRegExp rx(
"\\W*" + device +
":\\D*(\\d+)(?:\\D+\\d+){7}\\D+(\\d+)", Qt::CaseInsensitive);
161 in = rx.cap(1).toULong();
162 out = rx.cap(2).toULong();
164 kDebug() <<
"Network sensor: can not find " << device ;
173 void NetworkSensor::getIPAddress()
175 const QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
177 foreach (
const QNetworkInterface &interface, interfaces) {
178 if (device == interface.name()) {
179 QList<QNetworkAddressEntry> entries = interface.addressEntries();
180 if (entries.count() > 0) {
181 ipAddress = entries[0].ip().toString();
186 struct ifaddrs *ifa = NULL, *ifp = NULL;
188 if (getifaddrs (&ifp) < 0) {
189 kDebug() <<
"getifaddrs failed" ;
194 for (ifa = ifp; ifa; ifa = ifa->ifa_next) {
198 if (ifa->ifa_addr->sa_family == AF_INET) {
199 salen =
sizeof (
struct sockaddr_in);
200 }
else if (ifa->ifa_addr->sa_family == AF_INET6) {
208 if (getnameinfo(ifa->ifa_addr, salen, ip, sizeof (ip), NULL, 0, NI_NUMERICHOST) < 0) {
209 kDebug() <<
"getnameinfo < 0" ;
213 if (device == QString::fromLatin1(ifa->ifa_name)) {
214 ipAddress = QString::fromLatin1(ip);
232 unsigned long inB, outB;
233 const double delay = (double) netTimer.elapsed();
236 getInOutBytes(inB, outB);
244 decimals = (sp->
getParam(
"DECIMALS")).toInt();
245 if (format.length() == 0) {
249 format.replace(QRegExp(
"%inkb", Qt::CaseInsensitive),
250 QString::number(((inB - receivedBytes)*8) / delay,
'f', decimals));
252 format.replace(QRegExp(
"%in", Qt::CaseInsensitive),
253 QString::number((inB - receivedBytes) / delay,
'f', decimals));
255 format.replace(QRegExp(
"%outkb", Qt::CaseInsensitive),
256 QString::number(((outB - transmittedBytes)*8) / delay,
'f', decimals));
258 format.replace(QRegExp(
"%out", Qt::CaseInsensitive),
259 QString::number((outB - transmittedBytes) / delay,
'f', decimals));
261 format.replace(QRegExp(
"%dev", Qt::CaseInsensitive), device);
263 format.replace(QRegExp(
"%ip", Qt::CaseInsensitive), ipAddress);
269 transmittedBytes = outB;
272 #include "network.moc"
NetworkSensor(const QString &device, int interval)
QString getParam(const QString &) const
virtual void setValue(int)
QList< QObject * > * objList