Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<strings.h>
#include<time.h>
char *rip (char *s)
{
char *p;
if ((p = strrchr(s,'\n')) != NULL) *p = '\0';
if ((p = strrchr(s,'\r')) != NULL) *p = '\0';
return (s);
}
int main()
{
FILE *f;
char buf[100];
char tmp[16];
char cmd[100];
char host[50],commstr[30];
long int eth0in,eth0out,lo,itmp,etime;
long int peth0in,peth0out,plo,petime;//previous values
short int first=0;
strncpy(host,"localhost",sizeof(host));
strncpy(commstr,"public",sizeof(commstr));
sprintf(cmd,"snmpwalk %s %s interfaces.ifTable.ifEntry.ifInOctets.3 >/tmp/sysgrapheth0in",host,commstr);
system(cmd);
sprintf(cmd,"snmpwalk %s %s interfaces.ifTable.ifEntry.ifOutOctets.3 >/tmp/sysgrapheth0out",host,commstr);
system(cmd);
sprintf(cmd,"snmpwalk %s %s interfaces.ifTable.ifEntry.ifOutOctets.1 >/tmp/sysgraphlooutout",host,commstr);
system(cmd);
system("perl time.pl >/tmp/sysgraphtime");
system("/sbin/clock >/tmp/sysgraphclock");
f = fopen("/tmp/sysgraphtime","r");
fgets(buf,sizeof(buf),f);
fclose(f);
etime = atoi(buf);
f = fopen("/tmp/sysgrapheth0in","r");
fgets(buf,sizeof(buf),f);
fclose(f);
tmp[0] = buf[53];
tmp[1] = buf[54];
tmp[2] = buf[55];
tmp[3] = buf[56];
tmp[4] = buf[57];
tmp[5] = buf[58];
tmp[6] = buf[59];
tmp[7] = buf[60];
tmp[8] = buf[61];
tmp[9] = buf[62];
tmp[10] = buf[63];
eth0in=atol(rip(tmp));
printf("eth0in %s %d\n",rip(tmp),eth0in);
f = fopen("/tmp/sysgrapheth0out","r");
fgets(buf,sizeof(buf),f);
fclose(f);
tmp[0] = buf[54];
tmp[1] = buf[55];
tmp[2] = buf[56];
tmp[3] = buf[57];
tmp[4] = buf[58];
tmp[5] = buf[59];
tmp[6] = buf[60];
tmp[7] = buf[61];
tmp[8] = buf[62];
tmp[9] = buf[63];
eth0out=atol(rip(tmp));
printf("eth0out %s %d\n",rip(tmp),eth0out);
f = fopen("/tmp/sysgraphlooutout","r");
fgets(buf,sizeof(buf),f);
fclose(f);
tmp[0] = buf[54];
tmp[1] = buf[55];
tmp[2] = buf[56];
tmp[3] = buf[57];
tmp[4] = buf[58];
tmp[5] = buf[59];
tmp[6] = buf[60];
tmp[7] = buf[61];
tmp[8] = buf[62];
tmp[9] = buf[63];
lo=atol(rip(tmp));
printf("lo %s %d\n",rip(tmp),lo);
//time
itmp=etime;
etime=etime-petime;
petime=itmp;
//eth0in
itmp=eth0in;
eth0in=eth0in-peth0in;
peth0in=itmp;
eth0in=eth0in/etime; //bytes per second
//eth0out
itmp=eth0out;
eth0out=eth0out-peth0out;
peth0out=itmp;
eth0out=eth0out/etime; //bytes per second
//lo
itmp=lo;
lo=lo-plo;
plo=itmp;
lo=lo/etime; //bytes per second
printf("\n >>>eth0 in %d bps\n >>>eth0 out %d bps\n >>>lo %d bps\n\n",eth0in,eth0out,lo);
return 0;
}
Thanks,