-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifstats
executable file
·101 lines (86 loc) · 2.31 KB
/
ifstats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#! /bin/sh
# ifstats - print interface statistics (counters) on Linux
#
# Copyright (C) 2017,2024 by Erik Auerswald <[email protected]>
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved. This file is offered as-is,
# without any warranty.
set -u
PROG="$(basename "$0")"
VERSION='2024-09-22-01'
print_version()
{
printf -- '%s version %s\n' "${PROG}" "${VERSION}"
}
print_copyright()
{
echo 'Copyright (C) 2017,2024 by Erik Auerswald <[email protected]>'
}
print_license()
{
cat <<EOL
Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved. This file is offered as-is,
without any warranty.
EOL
}
print_usage()
{
cat <<EOU
Usage: ${PROG} { -h | -V | -L }
${PROG} [-n] [INTERFACE...]
EOU
}
print_help()
{
print_version
print_copyright
print_license
echo 'print interface statistics (counters) on Linux'
print_usage
cat <<EOH
Options:
-h print this help and exit
-V print version information and exit
-L print license and exit
-n omit zero count statistics
Examples:
${PROG}
${PROG} eth0
${PROG} eth1 eth2
EOH
}
SYS_CLASS_NET='/sys/class/net'
EXITCODE=0
ALLPATTERN='.'
ZEROPATTERN='^0'
GREPOPTS='-H'
GREPPAT=${ALLPATTERN}
while getopts ':hVLn' OPT; do
case "${OPT}" in
'h') print_help; exit 0;;
'V') print_version; print_copyright; exit 0;;
'L') print_version; print_copyright; echo; print_license; exit 0;;
'n') GREPOPTS="${GREPOPTS} -v"; GREPPAT=${ZEROPATTERN};;
'?') echo "${PROG}: error: unknown option '-$OPTARG'";
print_usage; exit 1;;
':') echo "${PROG}: error: argument required for option '-$OPTARG'";
print_usage; exit 1;;
*) echo "${PROG}: error: getopts() failure"; exit 1;;
esac
done
shift $((OPTIND - 1))
cd "${SYS_CLASS_NET}"
test $# -eq 0 && ifnames=$(ls -1) || ifnames="$*"
for i in ${ifnames}; do
test -d "${i}/statistics" ||
{ echo "% error: no interface ${i}"; EXITCODE=1 continue; }
echo "interface ${i}:"
cd "${i}/statistics"
grep ${GREPOPTS} -- "${GREPPAT}" * | sed 's/^/ /;s/:\([0-9]\)/: \1/'
cd "${SYS_CLASS_NET}"
done
exit "${EXITCODE}"