-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetmeter
executable file
·164 lines (124 loc) · 4.99 KB
/
netmeter
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/python
# Copyright (C) 2012 tdubourg, License: see LICENSE file in same folder
# 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, version 3 of the License
# 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 for more details.
# You should have received a copy of the GNU General Public License v3
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from signal import alarm
from signal import pause
import signal
from netmeter_config import *
# Utilities functions:
def doNothing(arg1, arg2):
global DBG
if DBG:
print "Doing nothing"
return;
def replace_content_in_file(fname, newcontent):
with open(fname, "w+") as f:
f.write(str(newcontent))
def open_in_read_or_create(fname):
try:
f = open(fname, "r+")
except IOError as e:
f = open(fname, "w+")
return f
interfaces = get_interfaces()
if DBG:
print interfaces
signal.signal(signal.SIGALRM, doNothing)
def put_session_in_total_count_for_interface(i):
f_tot_rcv = open_in_read_or_create(fname_with_interface(FNAME_TOTAL_RCV, i))
f_tot_sent = open_in_read_or_create(fname_with_interface(FNAME_TOTAL_SENT, i))
f_sess_rcv = open_in_read_or_create(fname_with_interface(FNAME_SESS_RCV, i))
f_sess_sent = open_in_read_or_create(fname_with_interface(FNAME_SESS_SENT, i))
try:
# if DBG:
# print "For interface", i, "former value was: up", int(f_sess_rcv.readline()), 'down', int(f_sess_sent.readline())
try:
prev_total_rcv = int(f_tot_rcv.readline())
prev_total_sent = int(f_tot_sent.readline())
except ValueError:
prev_total_rcv = 0
prev_total_sent = 0
total_rcv = prev_total_rcv + int(f_sess_rcv.readline())
total_sent = prev_total_sent + int(f_sess_sent.readline())
except ValueError as e:
if DBG:
print e
total_rcv = 0
total_sent = 0
f_tot_rcv.close()
f_tot_sent.close()
f_sess_sent.close()
f_sess_rcv.close()
# Updating the TOTAL files:
if DBG:
print "Replacing totals by: down", total_rcv, "up", total_sent
replace_content_in_file(fname_with_interface(FNAME_TOTAL_RCV, i), total_rcv)
replace_content_in_file(fname_with_interface(FNAME_TOTAL_SENT, i), total_sent)
# Reset of session files:
replace_content_in_file(fname_with_interface(FNAME_SESS_RCV, i), 0)
replace_content_in_file(fname_with_interface(FNAME_SESS_SENT, i), 0)
# Updating the total value by adding the vlue of last session
former_values = {}
EMPTY_INTERFACE = lambda: {"rcv": 0, "sent": 0}
def create_interface(i):
global former_values;
former_values[i] = EMPTY_INTERFACE()
for i in interfaces:
create_interface(i);
put_session_in_total_count_for_interface(i)
session_recv = 0
session_sent = 0
()
# Start the never-ending story !
while True:
if DBG:
print "looping"
with open(FNAME_MONITOR) as f:
for l in f.readlines():
m = r_interface_line.search(l)
if m is not None:
interface_name = m.group(1)
if DBG:
print "Current interface:", interface_name
print "Group:", m.group(2), "\n\n"
m = r_figure.findall(m.group(2))
if m is not None:
session_recv = int(m[0])
session_sent = int(m[8])
# If the connection has been reset without restarting the computer
# Then put the current session in the total counts before resetting it
try:
if session_recv < former_values[interface_name]["rcv"] or session_sent < former_values[interface_name]["sent"]:
if DBG:
print "Connection was reset for interface", interface_name
print "Former vs. new values (rcv, sent) are: (", former_values[interface_name]["rcv"], ",", former_values[interface_name]["sent"] ,") vs (", session_recv, ",", session_sent, ")"
put_session_in_total_count_for_interface(interface_name)
except KeyError as e:
if DBG:
print "New interface ", interface_name, " was added. Pushing to Totals possible session remaining and starting a new session"
put_session_in_total_count_for_interface(interface_name)
former_values[interface_name] = EMPTY_INTERFACE()
if DBG:
print "Updating former values for interface", interface_name, "..."
former_values[interface_name]["rcv"] = session_recv
former_values[interface_name]["sent"] = session_sent
if DBG:
print "Updating former values for interface", interface_name, "updated to:", former_values[interface_name]["rcv"], ",", former_values[interface_name]["sent"]
replace_content_in_file(fname_with_interface(FNAME_SESS_RCV, interface_name), session_recv)
replace_content_in_file(fname_with_interface(FNAME_SESS_SENT, interface_name), session_sent)
if DBG:
print "Received:", session_recv, "Sent:", session_sent
if DBG:
print former_values
elif DBG:
print "The pattern was not matched against the line l=", l
alarm(INTERVAL)
pause()