Diff
checker
Testo
Testo
Immagini
Documenti
Excel
Cartelle
Legal
Enterprise
Applicazione per desktop
Prezzi
Accedi
Scarica Diffchecker Desktop
Confronta il testo
Trova la differenza tra due file di testo
Strumenti
Cronologia
Visualizzazione
Layout
Diviso
Unificato
Editor live
Nascondi spazi bianchi
Comprimi invariate
Senza a capo
Livello di dettaglio
Intelligente
Sintassi
Nessuno
Modifica aspetto
Elaborazione
Ignora
Trasforma testo
Vai alla prima modifica
Modifica input
Diffchecker Desktop
Il modo più sicuro per usare Diffchecker. Ottieni l'app Diffchecker Desktop: i tuoi diff non lasciano mai il tuo computer!
Ottieni Desktop
Untitled diff
Creato
11 anni fa
Il diff non scade mai
Eliminare
Esporta
Condividere
Spiegare
219 rimozioni
Linee
Totale
Rimosso
Caratteri
Totale
Rimosso
Per continuare a utilizzare questa funzione, aggiorna a
Diff
checker
Pro
Visualizza prezzi
255 linee
Copia tutti
143 aggiunte
Linee
Totale
Aggiunto
Caratteri
Totale
Aggiunto
Per continuare a utilizzare questa funzione, aggiorna a
Diff
checker
Pro
Visualizza prezzi
321 linee
Copia tutti
Copia
Copiato
Copia
Copiato
/*
/*
opyright (C) 2010 Ciriaco Garcia de Celis
* C
opyright (C) 2010 Ciriaco Garcia de Celis
*
*
* This program is free software: you can redistribute it and/or
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
* the License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License (GPL) for more details.
* GNU General Public License (GPL) for more details.
*
*
* You should have received a copy of the GNU GPL along with this
* You should have received a copy of the GNU GPL along with this
* program. If not, see <http://www.gnu.org/licenses/>.
* program. If not, see <http://www.gnu.org/licenses/>.
*/
*/
// compile with "g++ -O3 -lrt netmon.cpp -o netmon"
// compile with "g++ -O3 -lrt netmon.cpp -o netmon"
#include <sys/stat.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <fcntl.h>
#include <unistd.h>
#include <unistd.h>
#include <cstdio>
#include <cstdio>
#include <cstring>
#include <cstring>
#include <cstdlib>
#include <cstdlib>
#include <ctime>
#include <ctime>
#include <climits>
#include <climits>
Copia
Copiato
Copia
Copiato
#include <cctype>
#define STATE_FILE_BASE "/dev/shm/netmon"
#define STATE_FILE_BASE "/dev/shm/netmon"
Copia
Copiato
Copia
Copiato
int exitapp(int exitcode)
{
int exitapp(int exitcode)
{
char errmsg[64];
char errmsg[64];
sprintf(errmsg, "ERROR code %d", exitcode);
sprintf(errmsg, "ERROR code %d", exitcode);
write(1, errmsg, strlen(errmsg));
write(1, errmsg, strlen(errmsg));
Copia
Copiato
Copia
Copiato
exit(exitcode);
Copia
Copiato
Copia
Copiato
return exitcode;
return exitcode;
}
}
Copia
Copiato
Copia
Copiato
int main(int argc, char** argv)
const int BLOCK_SIZE = 512;
{
if (argc < 2)
struct disk_stat {
{
long long read, write;
printf("usage: %s <network_interface>
[CPU]
\n", argv[0]);
double r_rate, w_rate;
char name[16];
};
const int MAX_STAT = 32;
class disk_stats {
disk_stat stat[MAX_STAT];
int total;
public:
disk_stats() : total(0) {}
const char* parse(const char* line, double seconds) {
if(total>=MAX_STAT) exitapp(11);
static char cad[256], buffer[256];
long long dummy;
disk_stat &ds = stat[total];
sscanf(line,"%lld %lld %s %lld %lld %lld %lld %lld %lld %lld",
&dummy,&dummy,ds.name,&dummy,&dummy,&ds.read,&dummy,&dummy,&dummy,&ds.write);
int len = strlen(ds.name);
if(!dummy) return 0;
if(isdigit(ds.name[len-1])) return 0;
long long prev_read = 0, prev_write = 0;
sprintf(cad,"%s.%s.%d",STATE_FILE_BASE,ds.name,getuid());
int fd = open(cad, O_RDWR | O_CREAT, 0664);
if (fd < 0) exitapp(9);
int bytes = read(fd, buffer, sizeof(buffer)-1);
if (bytes < 0) exitapp(10);
buffer[bytes] = 0;
sscanf(buffer,"%lld %lld",&prev_read,&prev_write);
lseek(fd, 0, SEEK_SET);
sprintf(buffer,"%lld %lld",ds.read,ds.write);
write(fd, buffer, 64);
close(fd);
ds.r_rate = (ds.read-prev_read)/seconds*BLOCK_SIZE/1024/1024;
ds.w_rate = (ds.write-prev_write)/seconds*BLOCK_SIZE/1024/1024;
if(ds.r_rate>0) {
if(ds.w_rate>0) sprintf(buffer," | %s: %3.0lf %3.0lf",ds.name,ds.r_rate,ds.w_rate);
else sprintf(buffer," | %s: %3.0lf .",ds.name,ds.r_rate);
} else {
if(ds.w_rate>0) sprintf(buffer," | %s: . %3.0lf",ds.name,ds.w_rate);
else sprintf(buffer," | %s: . .",ds.name);
}
++total;
return buffer;
}
};
int main(int argc, char** argv) {
if (argc < 2) {
printf("usage: %s <network_interface>
\n", argv[0]);
return 1;
return 1;
}
}
Copia
Copiato
Copia
Copiato
bool reportCPU = (argc > 2) && (strcmp(argv[2], "CPU") == 0);
Copia
Copiato
Copia
Copiato
char buffer[4096], cad[256], *ni, *nf;
char buffer[4096], cad[256], *ni, *nf;
// read network information
// read network information
int fd = open("/proc/net/dev", O_RDONLY);
int fd = open("/proc/net/dev", O_RDONLY);
if (fd < 0) return exitapp(2);
if (fd < 0) return exitapp(2);
int bytes = read(fd, buffer, sizeof(buffer)-1);
int bytes = read(fd, buffer, sizeof(buffer)-1);
close(fd);
close(fd);
if (bytes < 0) return exitapp(3);
if (bytes < 0) return exitapp(3);
buffer[bytes] = 0;
buffer[bytes] = 0;
Copia
Copiato
Copia
Copiato
timespec tp;
timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
clock_gettime(CLOCK_MONOTONIC, &tp);
long long nanoseconds = tp.tv_sec * 1000000000LL + tp.tv_nsec;
long long nanoseconds = tp.tv_sec * 1000000000LL + tp.tv_nsec;
Copia
Copiato
Copia
Copiato
long long recv_bytes=LLONG_MAX, sent_bytes=LLONG_MAX;
long long recv_bytes=LLONG_MAX, sent_bytes=LLONG_MAX;
bool networkAvailable = false;
bool networkAvailable = false;
Copia
Copiato
Copia
Copiato
// search for the proper network interface
// search for the proper network interface
strcpy(cad, argv[1]);
strcpy(cad, argv[1]);
strcat(cad, ":");
strcat(cad, ":");
char *pif = strstr(buffer, cad);
char *pif = strstr(buffer, cad);
if (pif
!= NULL)
if (pif
)
{
{
networkAvailable = true;
networkAvailable = true;
Copia
Copiato
Copia
Copiato
// jump to the received bytes field
// jump to the received bytes field
ni = pif + strlen(cad);
ni = pif + strlen(cad);
while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++;
while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++;
for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++);
for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++);
*nf++ = 0;
*nf++ = 0;
Copia
Copiato
Copia
Copiato
// get the received bytes
// get the received bytes
recv_bytes = atoll(ni);
recv_bytes = atoll(ni);
Copia
Copiato
Copia
Copiato
// jump to the sent bytes field
// jump to the sent bytes field
for (int skip = 0; skip < 8; skip++)
for (int skip = 0; skip < 8; skip++)
{
{
ni = nf;
ni = nf;
while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++;
while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++;
for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++);
for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++);
if (!*nf) break;
if (!*nf) break;
*nf++ = 0;
*nf++ = 0;
}
}
Copia
Copiato
Copia
Copiato
// get the sent bytes
// get the sent bytes
sent_bytes = atoll(ni);
sent_bytes = atoll(ni);
}
}
Copia
Copiato
Copia
Copiato
long long user_mode_time=0, user_mode_nice_time=0, system_mode_time=0, idle_time=0;
long long user_mode_time=0, user_mode_nice_time=0, system_mode_time=0, idle_time=0;
Copia
Copiato
Copia
Copiato
if (reportCPU)
{
Copia
Copiato
Copia
Copiato
// read CPU information
// read CPU information
fd = open("/proc/stat", O_RDONLY);
fd = open("/proc/stat", O_RDONLY);
if (fd < 0) return exitapp(4);
if (fd < 0) return exitapp(4);
bytes = read(fd, buffer, sizeof(buffer)-1);
bytes = read(fd, buffer, sizeof(buffer)-1);
close(fd);
close(fd);
if (bytes < 0) return exitapp(5);
if (bytes < 0) return exitapp(5);
buffer[bytes] = 0;
buffer[bytes] = 0;
Copia
Copiato
Copia
Copiato
pif = strstr(buffer, "cpu ");
pif = strstr(buffer, "cpu ");
if (pif
!= NULL)
if (pif
)
{
{
ni = pif + 3;
ni = pif + 3;
while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++;
while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++;
for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++);
for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++);
*nf++ = 0;
*nf++ = 0;
Copia
Copiato
Copia
Copiato
// get the user mode time
// get the user mode time
user_mode_time = atoll(ni);
user_mode_time = atoll(ni);
Copia
Copiato
Copia
Copiato
ni = nf;
ni = nf;
while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++;
while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++;
for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++);
for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++);
*nf++ = 0;
*nf++ = 0;
Copia
Copiato
Copia
Copiato
// get the user mode nice time
// get the user mode nice time
user_mode_nice_time = atoll(ni);
user_mode_nice_time = atoll(ni);
Copia
Copiato
Copia
Copiato
ni = nf;
ni = nf;
while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++;
while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++;
for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++);
for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++);
*nf++ = 0;
*nf++ = 0;
Copia
Copiato
Copia
Copiato
// get the system mode time
// get the system mode time
system_mode_time = atoll(ni);
system_mode_time = atoll(ni);
Copia
Copiato
Copia
Copiato
ni = nf;
ni = nf;
while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++;
while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++;
for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++);
for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++);
*nf++ = 0;
*nf++ = 0;
Copia
Copiato
Copia
Copiato
// get the idle time
// get the idle time
idle_time = atoll(ni);
idle_time = atoll(ni);
}
}
Copia
Copiato
Copia
Copiato
}
Copia
Copiato
Copia
Copiato
// read the received/sent bytes, date and CPU usage stored by a previous execution
// read the received/sent bytes, date and CPU usage stored by a previous execution
sprintf(cad, "%s.%s.%d", STATE_FILE_BASE, argv[1], getuid());
sprintf(cad, "%s.%s.%d", STATE_FILE_BASE, argv[1], getuid());
fd = open(cad, O_RDWR | O_CREAT, 0664);
fd = open(cad, O_RDWR | O_CREAT, 0664);
if (fd < 0) return exitapp(6);
if (fd < 0) return exitapp(6);
bytes = read(fd, buffer, sizeof(buffer)-1);
bytes = read(fd, buffer, sizeof(buffer)-1);
if (bytes < 0)
if (bytes < 0)
{
{
close(fd);
close(fd);
return exitapp(7);
return exitapp(7);
}
}
long long prev_recv_bytes
, prev_sent_bytes
, prev_nanoseconds = -1;
long long prev_recv_bytes
= 0
, prev_sent_bytes
= 0
, prev_nanoseconds = -1;
long long prev_user_mode_time
, prev_user_mode_nice_time
, prev_system_mode_time
, prev_idle_time = -1;
long long prev_user_mode_time
= 0
, prev_user_mode_nice_time
= 0
, prev_system_mode_time
= 0
, prev_idle_time = -1;
if (bytes > 0)
if (bytes > 0)
{
{
prev_recv_bytes = atoll(buffer);
prev_recv_bytes = atoll(buffer);
prev_sent_bytes = atoll(buffer+20);
prev_sent_bytes = atoll(buffer+20);
prev_nanoseconds = atoll(buffer+40);
prev_nanoseconds = atoll(buffer+40);
prev_user_mode_time = atoll(buffer+60);
prev_user_mode_time = atoll(buffer+60);
prev_user_mode_nice_time = atoll(buffer+80);
prev_user_mode_nice_time = atoll(buffer+80);
prev_system_mode_time = atoll(buffer+100);
prev_system_mode_time = atoll(buffer+100);
prev_idle_time = atoll(buffer+120);
prev_idle_time = atoll(buffer+120);
}
}
Copia
Copiato
Copia
Copiato
// store in the file the current values for later use
// store in the file the current values for later use
sprintf(buffer, "%019lld\n%019lld\n%019lld\n%019lld\n%019lld\n%019lld\n%019lld\n",
sprintf(buffer, "%019lld\n%019lld\n%019lld\n%019lld\n%019lld\n%019lld\n%019lld\n",
recv_bytes, sent_bytes, nanoseconds,
recv_bytes, sent_bytes, nanoseconds,
user_mode_time, user_mode_nice_time, system_mode_time, idle_time);
user_mode_time, user_mode_nice_time, system_mode_time, idle_time);
lseek(fd, 0, SEEK_SET);
lseek(fd, 0, SEEK_SET);
write(fd, buffer, 140);
write(fd, buffer, 140);
close(fd);
close(fd);
Copia
Copiato
Copia
Copiato
// generate the result
// generate the result
Copia
Copiato
Copia
Copiato
strcpy(buffer, "<txt>");
strcpy(buffer, "<txt>");
Copia
Copiato
Copia
Copiato
Text moved with changes to lines 228-237 (91.8% similarity)
bool hasNet = networkAvailable && (prev_nanoseconds >= 0) && (recv_bytes >= prev_recv_bytes) && (sent_bytes >= prev_sent_bytes);
if (!networkAvailable)
{
sprintf(cad, " %s is down", argv[1]);
strcat(buffer, cad);
}
else if (!hasNet)
{
strcat(buffer, " ? kbps IN \n ? kbps OUT");
}
else
{
Copia
Copiato
Copia
Copiato
long long elapsed = nanoseconds - prev_nanoseconds;
long long elapsed = nanoseconds - prev_nanoseconds;
if (elapsed < 1) elapsed = 1;
if (elapsed < 1) elapsed = 1;
double seconds = elapsed / 1000000000.0;
double seconds = elapsed / 1000000000.0;
Copia
Copiato
Copia
Copiato
Text moved with changes from lines 180-193 (91.8% similarity)
// NETWORK
bool hasNet = networkAvailable && (prev_nanoseconds >= 0) && (recv_bytes >= prev_recv_bytes) && (sent_bytes >= prev_sent_bytes);
if (!networkAvailable) {
sprintf(cad, " %s is down", argv[1]);
strcat(buffer, cad);
} else if (!hasNet) {
strcat(buffer, "D: - U: - ");
} else {
Copia
Copiato
Copia
Copiato
long long sent = sent_bytes - prev_sent_bytes;
long long sent = sent_bytes - prev_sent_bytes;
long long received = recv_bytes - prev_recv_bytes;
long long received = recv_bytes - prev_recv_bytes;
long inbps = (long) (8 * received / seconds + 999)
; // adding 999 ensures "1" for any rate above 0
long inbps = (long) (8 * received / seconds + 999)
/ 8 / 1024
; // adding 999 ensures "1" for any rate above 0
long outbps = (long) (8 * sent / seconds + 999)
;
long outbps = (long) (8 * sent / seconds + 999)
/ 8 / 1024
;
if (inbps <
1000000)
sprintf(cad, "
%6d kbps IN \n
", inbps
/1000
);
if (inbps <
1024)
sprintf(cad, "
D: %5ld KB/s
", inbps
);
else
else
sprintf(cad, "
D: %5.0lf MB/s
", inbps/
1024
.0);
sprintf(cad, "
%6.3f Mbps IN \n
", inbps/
1000000
.0);
strcat(buffer, cad);
strcat(buffer, cad);
Copia
Copiato
Copia
Copiato
if (outbps <
1000000)
if (outbps <
1024)
sprintf(cad, "
| U: %5ld KB/s
", outbps
);
sprintf(cad, "
%6d kbps OUT
", outbps
/1000
);
else
sprintf(cad, "
| U: %5.0lf MB/s
", outbps/
1024
.0);
else
sprintf(cad, "
%6.3f Mbps OUT
", outbps/
1000000
.0);
strcat(buffer, cad);
strcat(buffer, cad);
}
}
Copia
Copiato
Copia
Copiato
long long cpu_used = user_mode_time + user_mode_nice_time + system_mode_time
long long cpu_used = user_mode_time + user_mode_nice_time + system_mode_time
- (prev_user_mode_time + prev_user_mode_nice_time + prev_system_mode_time);
- (prev_user_mode_time + prev_user_mode_nice_time + prev_system_mode_time);
Copia
Copiato
Copia
Copiato
long long total_cpu = cpu_used + (idle_time - prev_idle_time);
long long total_cpu = cpu_used + (idle_time - prev_idle_time);
Copia
Copiato
Copia
Copiato
int total_ram = 0, ram_used = 0;
Copia
Copiato
Copia
Copiato
bool hasCPU = (prev_idle_time >= 0) && (total_cpu > 0);
bool hasCPU = (prev_idle_time >= 0) && (total_cpu > 0);
if (reportCPU)
{
// CPU
if (!hasCPU)
if (!hasCPU) sprintf(cad, " | CPU: %3lld",total_cpu);
{
else sprintf(cad, " | CPU: %3.0lf%%", cpu_used * 100.0 / total_cpu);
strcat(buffer, "\n ? % CPU");
strcat(buffer, cad);
// RAM
FILE *meminfo = fopen("/proc/meminfo", "r");
if(meminfo) {
char line[256];
while(fgets(line, sizeof(line), meminfo)) {
if(!total_ram && sscanf(line, "MemTotal: %d kB", &total_ram));
if(sscanf(line, "Active: %d kB", &ram_used) == 1) {
sprintf(cad," | RAM: %5d MB",ram_used/1024);
break;
}
}
else
}
{
fclose(meminfo);
sprintf(cad, "
\n
%5
.1f%% CPU", cpu_used * 100.0 /
total_
cpu);
if(!ram_used)
sprintf(cad, "
RAM:
%5
d MB",
total_
ram);
strcat(buffer,cad);
}
// DISK
FILE *diskstat = fopen("/proc/diskstats","rt");
disk_stats dss;
if(diskstat) {
char line[512];
while(fgets(line,sizeof(line),diskstat)) {
const char *cad = dss.parse(line,seconds);
if(!cad) continue;
strcat(buffer, cad);
strcat(buffer, cad);
}
}
Copia
Copiato
Copia
Copiato
fclose(diskstat);
Copia
Copiato
Copia
Copiato
}
}
Copia
Copiato
Copia
Copiato
strcat(buffer, "</txt><tool>");
strcat(buffer, "</txt><tool>");
Copia
Copiato
Copia
Copiato
if (
networkAvailable && hasNet
)
{
// TOOLTIPS
bool useNet =
networkAvailable && hasNet
;
if (useNet)
{
sprintf(cad, " %s:\n %.2f MB received \n %.2f MB sent ",
sprintf(cad, " %s:\n %.2f MB received \n %.2f MB sent ",
argv[1], recv_bytes/
1000000.0
, sent_bytes/
1000000.0
);
argv[1], recv_bytes/
1024.0/1024
, sent_bytes/
1024.0/1024
);
strcat(buffer, cad);
strcat(buffer, cad);
}
}
Copia
Copiato
Copia
Copiato
if (
reportCPU &&
hasCPU)
if (
hasCPU)
{
{
if (
use
Net) strcat(buffer, "\n");
if (
networkAvailable && has
Net) strcat(buffer, "\n");
long long total_used_cpu = user_mode_time + user_mode_nice_time + system_mode_time;
long long total_used_cpu = user_mode_time + user_mode_nice_time + system_mode_time;
sprintf(cad, " CPU usage:\n %5.1f%% since boot
",
sprintf(cad, " CPU usage:\n %5.1f%% since boot
\n
",
total_used_cpu * 100.0 / (total_used_cpu + idle_time));
total_used_cpu * 100.0 / (total_used_cpu + idle_time));
strcat(buffer, cad);
strcat(buffer, cad);
}
}
Copia
Copiato
Copia
Copiato
sprintf(cad, " RAM usage:\n %5.1f%% of %d MB",
ram_used * 100.0 / total_ram, total_ram / 1024 );
strcat(buffer, cad);
Copia
Copiato
Copia
Copiato
strcat(buffer, "</tool>");
strcat(buffer, "</tool>");
Copia
Copiato
Copia
Copiato
write(1, buffer, strlen(buffer));
write(1, buffer, strlen(buffer));
Copia
Copiato
Copia
Copiato
return 0;
return 0;
}
}
Diff salvati
Testo originale
Apri file
/* * Copyright (C) 2010 Ciriaco Garcia de Celis * * 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 3 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License (GPL) for more details. * * You should have received a copy of the GNU GPL along with this * program. If not, see <http://www.gnu.org/licenses/>. */ // compile with "g++ -O3 -lrt netmon.cpp -o netmon" #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <cstdio> #include <cstring> #include <cstdlib> #include <ctime> #include <climits> #define STATE_FILE_BASE "/dev/shm/netmon" int exitapp(int exitcode) { char errmsg[64]; sprintf(errmsg, "ERROR code %d", exitcode); write(1, errmsg, strlen(errmsg)); return exitcode; } int main(int argc, char** argv) { if (argc < 2) { printf("usage: %s <network_interface> [CPU]\n", argv[0]); return 1; } bool reportCPU = (argc > 2) && (strcmp(argv[2], "CPU") == 0); char buffer[4096], cad[256], *ni, *nf; // read network information int fd = open("/proc/net/dev", O_RDONLY); if (fd < 0) return exitapp(2); int bytes = read(fd, buffer, sizeof(buffer)-1); close(fd); if (bytes < 0) return exitapp(3); buffer[bytes] = 0; timespec tp; clock_gettime(CLOCK_MONOTONIC, &tp); long long nanoseconds = tp.tv_sec * 1000000000LL + tp.tv_nsec; long long recv_bytes=LLONG_MAX, sent_bytes=LLONG_MAX; bool networkAvailable = false; // search for the proper network interface strcpy(cad, argv[1]); strcat(cad, ":"); char *pif = strstr(buffer, cad); if (pif != NULL) { networkAvailable = true; // jump to the received bytes field ni = pif + strlen(cad); while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++; for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++); *nf++ = 0; // get the received bytes recv_bytes = atoll(ni); // jump to the sent bytes field for (int skip = 0; skip < 8; skip++) { ni = nf; while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++; for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++); if (!*nf) break; *nf++ = 0; } // get the sent bytes sent_bytes = atoll(ni); } long long user_mode_time=0, user_mode_nice_time=0, system_mode_time=0, idle_time=0; if (reportCPU) { // read CPU information fd = open("/proc/stat", O_RDONLY); if (fd < 0) return exitapp(4); bytes = read(fd, buffer, sizeof(buffer)-1); close(fd); if (bytes < 0) return exitapp(5); buffer[bytes] = 0; pif = strstr(buffer, "cpu "); if (pif != NULL) { ni = pif + 3; while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++; for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++); *nf++ = 0; // get the user mode time user_mode_time = atoll(ni); ni = nf; while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++; for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++); *nf++ = 0; // get the user mode nice time user_mode_nice_time = atoll(ni); ni = nf; while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++; for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++); *nf++ = 0; // get the system mode time system_mode_time = atoll(ni); ni = nf; while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++; for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++); *nf++ = 0; // get the idle time idle_time = atoll(ni); } } // read the received/sent bytes, date and CPU usage stored by a previous execution sprintf(cad, "%s.%s.%d", STATE_FILE_BASE, argv[1], getuid()); fd = open(cad, O_RDWR | O_CREAT, 0664); if (fd < 0) return exitapp(6); bytes = read(fd, buffer, sizeof(buffer)-1); if (bytes < 0) { close(fd); return exitapp(7); } long long prev_recv_bytes, prev_sent_bytes, prev_nanoseconds = -1; long long prev_user_mode_time, prev_user_mode_nice_time, prev_system_mode_time, prev_idle_time = -1; if (bytes > 0) { prev_recv_bytes = atoll(buffer); prev_sent_bytes = atoll(buffer+20); prev_nanoseconds = atoll(buffer+40); prev_user_mode_time = atoll(buffer+60); prev_user_mode_nice_time = atoll(buffer+80); prev_system_mode_time = atoll(buffer+100); prev_idle_time = atoll(buffer+120); } // store in the file the current values for later use sprintf(buffer, "%019lld\n%019lld\n%019lld\n%019lld\n%019lld\n%019lld\n%019lld\n", recv_bytes, sent_bytes, nanoseconds, user_mode_time, user_mode_nice_time, system_mode_time, idle_time); lseek(fd, 0, SEEK_SET); write(fd, buffer, 140); close(fd); // generate the result strcpy(buffer, "<txt>"); bool hasNet = networkAvailable && (prev_nanoseconds >= 0) && (recv_bytes >= prev_recv_bytes) && (sent_bytes >= prev_sent_bytes); if (!networkAvailable) { sprintf(cad, " %s is down", argv[1]); strcat(buffer, cad); } else if (!hasNet) { strcat(buffer, " ? kbps IN \n ? kbps OUT"); } else { long long elapsed = nanoseconds - prev_nanoseconds; if (elapsed < 1) elapsed = 1; double seconds = elapsed / 1000000000.0; long long sent = sent_bytes - prev_sent_bytes; long long received = recv_bytes - prev_recv_bytes; long inbps = (long) (8 * received / seconds + 999); // adding 999 ensures "1" for any rate above 0 long outbps = (long) (8 * sent / seconds + 999); if (inbps < 1000000) sprintf(cad, "%6d kbps IN \n", inbps/1000); else sprintf(cad, "%6.3f Mbps IN \n", inbps/1000000.0); strcat(buffer, cad); if (outbps < 1000000) sprintf(cad, "%6d kbps OUT", outbps/1000); else sprintf(cad, "%6.3f Mbps OUT", outbps/1000000.0); strcat(buffer, cad); } long long cpu_used = user_mode_time + user_mode_nice_time + system_mode_time - (prev_user_mode_time + prev_user_mode_nice_time + prev_system_mode_time); long long total_cpu = cpu_used + (idle_time - prev_idle_time); bool hasCPU = (prev_idle_time >= 0) && (total_cpu > 0); if (reportCPU) { if (!hasCPU) { strcat(buffer, "\n ? % CPU"); } else { sprintf(cad, "\n %5.1f%% CPU", cpu_used * 100.0 / total_cpu); strcat(buffer, cad); } } strcat(buffer, "</txt><tool>"); if (networkAvailable && hasNet) { sprintf(cad, " %s:\n %.2f MB received \n %.2f MB sent ", argv[1], recv_bytes/1000000.0, sent_bytes/1000000.0); strcat(buffer, cad); } if (reportCPU && hasCPU) { if (networkAvailable && hasNet) strcat(buffer, "\n"); long long total_used_cpu = user_mode_time + user_mode_nice_time + system_mode_time; sprintf(cad, " CPU usage:\n %5.1f%% since boot ", total_used_cpu * 100.0 / (total_used_cpu + idle_time)); strcat(buffer, cad); } strcat(buffer, "</tool>"); write(1, buffer, strlen(buffer)); return 0; }
Testo modificato
Apri file
/* opyright (C) 2010 Ciriaco Garcia de Celis * * 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 3 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License (GPL) for more details. * * You should have received a copy of the GNU GPL along with this * program. If not, see <http://www.gnu.org/licenses/>. */ // compile with "g++ -O3 -lrt netmon.cpp -o netmon" #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <cstdio> #include <cstring> #include <cstdlib> #include <ctime> #include <climits> #include <cctype> #define STATE_FILE_BASE "/dev/shm/netmon" int exitapp(int exitcode) { char errmsg[64]; sprintf(errmsg, "ERROR code %d", exitcode); write(1, errmsg, strlen(errmsg)); exit(exitcode); return exitcode; } const int BLOCK_SIZE = 512; struct disk_stat { long long read, write; double r_rate, w_rate; char name[16]; }; const int MAX_STAT = 32; class disk_stats { disk_stat stat[MAX_STAT]; int total; public: disk_stats() : total(0) {} const char* parse(const char* line, double seconds) { if(total>=MAX_STAT) exitapp(11); static char cad[256], buffer[256]; long long dummy; disk_stat &ds = stat[total]; sscanf(line,"%lld %lld %s %lld %lld %lld %lld %lld %lld %lld", &dummy,&dummy,ds.name,&dummy,&dummy,&ds.read,&dummy,&dummy,&dummy,&ds.write); int len = strlen(ds.name); if(!dummy) return 0; if(isdigit(ds.name[len-1])) return 0; long long prev_read = 0, prev_write = 0; sprintf(cad,"%s.%s.%d",STATE_FILE_BASE,ds.name,getuid()); int fd = open(cad, O_RDWR | O_CREAT, 0664); if (fd < 0) exitapp(9); int bytes = read(fd, buffer, sizeof(buffer)-1); if (bytes < 0) exitapp(10); buffer[bytes] = 0; sscanf(buffer,"%lld %lld",&prev_read,&prev_write); lseek(fd, 0, SEEK_SET); sprintf(buffer,"%lld %lld",ds.read,ds.write); write(fd, buffer, 64); close(fd); ds.r_rate = (ds.read-prev_read)/seconds*BLOCK_SIZE/1024/1024; ds.w_rate = (ds.write-prev_write)/seconds*BLOCK_SIZE/1024/1024; if(ds.r_rate>0) { if(ds.w_rate>0) sprintf(buffer," | %s: %3.0lf %3.0lf",ds.name,ds.r_rate,ds.w_rate); else sprintf(buffer," | %s: %3.0lf .",ds.name,ds.r_rate); } else { if(ds.w_rate>0) sprintf(buffer," | %s: . %3.0lf",ds.name,ds.w_rate); else sprintf(buffer," | %s: . .",ds.name); } ++total; return buffer; } }; int main(int argc, char** argv) { if (argc < 2) { printf("usage: %s <network_interface>\n", argv[0]); return 1; } char buffer[4096], cad[256], *ni, *nf; // read network information int fd = open("/proc/net/dev", O_RDONLY); if (fd < 0) return exitapp(2); int bytes = read(fd, buffer, sizeof(buffer)-1); close(fd); if (bytes < 0) return exitapp(3); buffer[bytes] = 0; timespec tp; clock_gettime(CLOCK_MONOTONIC, &tp); long long nanoseconds = tp.tv_sec * 1000000000LL + tp.tv_nsec; long long recv_bytes=LLONG_MAX, sent_bytes=LLONG_MAX; bool networkAvailable = false; // search for the proper network interface strcpy(cad, argv[1]); strcat(cad, ":"); char *pif = strstr(buffer, cad); if (pif) { networkAvailable = true; // jump to the received bytes field ni = pif + strlen(cad); while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++; for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++); *nf++ = 0; // get the received bytes recv_bytes = atoll(ni); // jump to the sent bytes field for (int skip = 0; skip < 8; skip++) { ni = nf; while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++; for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++); if (!*nf) break; *nf++ = 0; } // get the sent bytes sent_bytes = atoll(ni); } long long user_mode_time=0, user_mode_nice_time=0, system_mode_time=0, idle_time=0; // read CPU information fd = open("/proc/stat", O_RDONLY); if (fd < 0) return exitapp(4); bytes = read(fd, buffer, sizeof(buffer)-1); close(fd); if (bytes < 0) return exitapp(5); buffer[bytes] = 0; pif = strstr(buffer, "cpu "); if (pif) { ni = pif + 3; while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++; for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++); *nf++ = 0; // get the user mode time user_mode_time = atoll(ni); ni = nf; while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++; for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++); *nf++ = 0; // get the user mode nice time user_mode_nice_time = atoll(ni); ni = nf; while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++; for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++); *nf++ = 0; // get the system mode time system_mode_time = atoll(ni); ni = nf; while (*ni && ((*ni == ' ') || (*ni == '\t'))) ni++; for (nf = ni; *nf && (*nf != ' ') && (*nf != '\t'); nf++); *nf++ = 0; // get the idle time idle_time = atoll(ni); } // read the received/sent bytes, date and CPU usage stored by a previous execution sprintf(cad, "%s.%s.%d", STATE_FILE_BASE, argv[1], getuid()); fd = open(cad, O_RDWR | O_CREAT, 0664); if (fd < 0) return exitapp(6); bytes = read(fd, buffer, sizeof(buffer)-1); if (bytes < 0) { close(fd); return exitapp(7); } long long prev_recv_bytes = 0, prev_sent_bytes = 0, prev_nanoseconds = -1; long long prev_user_mode_time = 0, prev_user_mode_nice_time = 0, prev_system_mode_time = 0, prev_idle_time = -1; if (bytes > 0) { prev_recv_bytes = atoll(buffer); prev_sent_bytes = atoll(buffer+20); prev_nanoseconds = atoll(buffer+40); prev_user_mode_time = atoll(buffer+60); prev_user_mode_nice_time = atoll(buffer+80); prev_system_mode_time = atoll(buffer+100); prev_idle_time = atoll(buffer+120); } // store in the file the current values for later use sprintf(buffer, "%019lld\n%019lld\n%019lld\n%019lld\n%019lld\n%019lld\n%019lld\n", recv_bytes, sent_bytes, nanoseconds, user_mode_time, user_mode_nice_time, system_mode_time, idle_time); lseek(fd, 0, SEEK_SET); write(fd, buffer, 140); close(fd); // generate the result strcpy(buffer, "<txt>"); long long elapsed = nanoseconds - prev_nanoseconds; if (elapsed < 1) elapsed = 1; double seconds = elapsed / 1000000000.0; // NETWORK bool hasNet = networkAvailable && (prev_nanoseconds >= 0) && (recv_bytes >= prev_recv_bytes) && (sent_bytes >= prev_sent_bytes); if (!networkAvailable) { sprintf(cad, " %s is down", argv[1]); strcat(buffer, cad); } else if (!hasNet) { strcat(buffer, "D: - U: - "); } else { long long sent = sent_bytes - prev_sent_bytes; long long received = recv_bytes - prev_recv_bytes; long inbps = (long) (8 * received / seconds + 999) / 8 / 1024; // adding 999 ensures "1" for any rate above 0 long outbps = (long) (8 * sent / seconds + 999) / 8 / 1024; if (inbps < 1024) sprintf(cad, "D: %5ld KB/s ", inbps); else sprintf(cad, "D: %5.0lf MB/s ", inbps/1024.0); strcat(buffer, cad); if (outbps < 1024) sprintf(cad, " | U: %5ld KB/s ", outbps); else sprintf(cad, " | U: %5.0lf MB/s ", outbps/1024.0); strcat(buffer, cad); } long long cpu_used = user_mode_time + user_mode_nice_time + system_mode_time - (prev_user_mode_time + prev_user_mode_nice_time + prev_system_mode_time); long long total_cpu = cpu_used + (idle_time - prev_idle_time); int total_ram = 0, ram_used = 0; bool hasCPU = (prev_idle_time >= 0) && (total_cpu > 0); // CPU if (!hasCPU) sprintf(cad, " | CPU: %3lld",total_cpu); else sprintf(cad, " | CPU: %3.0lf%%", cpu_used * 100.0 / total_cpu); strcat(buffer, cad); // RAM FILE *meminfo = fopen("/proc/meminfo", "r"); if(meminfo) { char line[256]; while(fgets(line, sizeof(line), meminfo)) { if(!total_ram && sscanf(line, "MemTotal: %d kB", &total_ram)); if(sscanf(line, "Active: %d kB", &ram_used) == 1) { sprintf(cad," | RAM: %5d MB",ram_used/1024); break; } } fclose(meminfo); if(!ram_used) sprintf(cad, " RAM: %5d MB",total_ram); strcat(buffer,cad); } // DISK FILE *diskstat = fopen("/proc/diskstats","rt"); disk_stats dss; if(diskstat) { char line[512]; while(fgets(line,sizeof(line),diskstat)) { const char *cad = dss.parse(line,seconds); if(!cad) continue; strcat(buffer, cad); } fclose(diskstat); } strcat(buffer, "</txt><tool>"); // TOOLTIPS bool useNet = networkAvailable && hasNet; if (useNet) { sprintf(cad, " %s:\n %.2f MB received \n %.2f MB sent ", argv[1], recv_bytes/1024.0/1024, sent_bytes/1024.0/1024); strcat(buffer, cad); } if (hasCPU) { if (useNet) strcat(buffer, "\n"); long long total_used_cpu = user_mode_time + user_mode_nice_time + system_mode_time; sprintf(cad, " CPU usage:\n %5.1f%% since boot \n", total_used_cpu * 100.0 / (total_used_cpu + idle_time)); strcat(buffer, cad); } sprintf(cad, " RAM usage:\n %5.1f%% of %d MB", ram_used * 100.0 / total_ram, total_ram / 1024 ); strcat(buffer, cad); strcat(buffer, "</tool>"); write(1, buffer, strlen(buffer)); return 0; }
Trovare la differenza