commit 62e66fce04b47d592ae7ae79b04d7deca80092dc
parent c1a0c5055310876235e7de09a120cf21db105001
Author: sin <sin@2f30.org>
Date: Fri, 19 Feb 2016 11:35:00 +0000
Add [-c count] option to limit the number of samples
Diffstat:
M | sbm.1 | | | 9 | ++++++++- |
M | sbm.c | | | 19 | ++++++++++++++++--- |
2 files changed, 24 insertions(+), 4 deletions(-)
diff --git a/sbm.1 b/sbm.1
@@ -1,4 +1,4 @@
-.Dd Feb 16, 2016
+.Dd Feb 19, 2016
.Dt SBM 1
.Os
.Sh NAME
@@ -7,6 +7,7 @@
.Sh SYNOPSIS
.Nm sbm
.Op Fl i Ar interface
+.Op Fl c Ar count
.Sh DESCRIPTION
.Nm
is a simple bandwidth monitor.
@@ -15,6 +16,12 @@ is a simple bandwidth monitor.
.It Fl i Ar interface
Monitor the selected interface. If not provided the first
active, non-loopback interface will be used.
+.It Fl c Ar count
+Stop monitoring after
+.Ar count
+samples. If
+.Ar count
+is 0, it will loop forever. This is the default.
.El
.Sh AUTHORS
.An Dimitris Papastamos Aq Mt sin@2f30.org
diff --git a/sbm.c b/sbm.c
@@ -27,7 +27,9 @@
#endif
#include <err.h>
+#include <errno.h>
#include <ifaddrs.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -175,12 +177,13 @@ print(char *ifname, unsigned long long rxbits, unsigned long long txbits,
}
void
-loop(char *ifname)
+loop(char *ifname, long count)
{
unsigned long long oldrxbytes, rxbytes;
unsigned long long oldtxbytes, txbytes;
unsigned long long oldrxpps, rxpps;
unsigned long long oldtxpps, txpps;
+ long n = 0;
sample(ifname, &oldrxbytes, &oldtxbytes, &oldrxpps, &oldtxpps);
for (;;) {
@@ -195,13 +198,15 @@ loop(char *ifname)
oldtxbytes = txbytes;
oldrxpps = rxpps;
oldtxpps = txpps;
+ if (count && ++n >= count)
+ break;
}
}
void
usage(void)
{
- fprintf(stderr, "usage: %s [-i interface]\n", argv0);
+ fprintf(stderr, "usage: %s [-c count] [-i interface]\n", argv0);
exit(1);
}
@@ -209,8 +214,16 @@ int
main(int argc, char *argv[])
{
char ifname[IF_NAMESIZE] = "";
+ char *end;
+ long count = 0;
ARGBEGIN {
+ case 'c':
+ errno = 0;
+ count = strtol(EARGF(usage()), &end, 10);
+ if (*end != '\0' || errno)
+ errx(1, "invalid count");
+ break;
case 'i':
strncpy(ifname, EARGF(usage()), sizeof(ifname));
ifname[IF_NAMESIZE - 1] = '\0';
@@ -220,6 +233,6 @@ main(int argc, char *argv[])
} ARGEND
scan(ifname);
- loop(ifname);
+ loop(ifname, count);
return 0;
}