summaryrefslogtreecommitdiff
path: root/examples/topip.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/topip.py')
-rwxr-xr-xexamples/topip.py54
1 files changed, 29 insertions, 25 deletions
diff --git a/examples/topip.py b/examples/topip.py
index 4f57067..cd9fbc9 100755
--- a/examples/topip.py
+++ b/examples/topip.py
@@ -64,6 +64,10 @@ PEXPECT LICENSE
'''
+from __future__ import print_function
+
+from __future__ import absolute_import
+
# See http://pexpect.sourceforge.net/
import pexpect
import pxssh
@@ -83,7 +87,7 @@ TOPIP_LAST_RUN_STATS = '/var/run/topip.last'
def exit_with_usage():
- print globals()['__doc__']
+ print(globals()['__doc__'])
os._exit(1)
def stats(r):
@@ -127,29 +131,29 @@ def main():
try:
optlist, args = getopt.getopt(sys.argv[1:],
'h?valqs:u:p:n:', ['help','h','?','ipv6','stddev='])
- except Exception, e:
- print str(e)
+ except Exception as e:
+ print(str(e))
exit_with_usage()
options = dict(optlist)
munin_flag = False
if len(args) > 0:
if args[0] == 'config':
- print 'graph_title Netstat Connections per IP'
- print 'graph_vlabel Socket connections per IP'
- print 'connections_max.label max'
- print 'connections_max.info Maximum number of connections per IP'
- print 'connections_avg.label avg'
- print 'connections_avg.info Average number of connections per IP'
- print 'connections_stddev.label stddev'
- print 'connections_stddev.info Standard deviation'
+ print('graph_title Netstat Connections per IP')
+ print('graph_vlabel Socket connections per IP')
+ print('connections_max.label max')
+ print('connections_max.info Maximum number of connections per IP')
+ print('connections_avg.label avg')
+ print('connections_avg.info Average number of connections per IP')
+ print('connections_stddev.label stddev')
+ print('connections_stddev.info Standard deviation')
return 0
elif args[0] != '':
- print args, len(args)
+ print(args, len(args))
return 0
exit_with_usage()
if [elem for elem in options if elem in ['-h','--h','-?','--?','--help']]:
- print 'Help:'
+ print('Help:')
exit_with_usage()
if '-s' in options:
hostname = options['-s']
@@ -236,7 +240,7 @@ def main():
# lambda x,y:cmp(x[1], y[1]),reverse=True)
ip_list = ip_list.items()
if len(ip_list) < 1:
- if verbose: print 'Warning: no networks connections worth looking at.'
+ if verbose: print('Warning: no networks connections worth looking at.')
return 0
ip_list.sort(lambda x,y:cmp(y[1],x[1]))
@@ -249,13 +253,13 @@ def main():
# print munin-style or verbose results for the stats.
if munin_flag:
- print 'connections_max.value', s['max']
- print 'connections_avg.value', s['avg']
- print 'connections_stddev.value', s['stddev']
+ print('connections_max.value', s['max'])
+ print('connections_avg.value', s['avg'])
+ print('connections_stddev.value', s['stddev'])
return 0
if verbose:
pprint (s)
- print
+ print()
pprint (ip_list[0:average_n])
# load the stats from the last run.
@@ -266,13 +270,13 @@ def main():
if ( s['maxip'][1] > (s['stddev'] * stddev_trigger)
and s['maxip']==last_stats['maxip'] ):
- if verbose: print 'The maxip has been above trigger for two consecutive samples.'
+ if verbose: print('The maxip has been above trigger for two consecutive samples.')
if alert_flag:
- if verbose: print 'SENDING ALERT EMAIL'
+ if verbose: print('SENDING ALERT EMAIL')
send_alert(str(s), 'ALERT on %s'
% hostname, alert_addr_from, alert_addr_to)
if log_flag:
- if verbose: print 'LOGGING THIS EVENT'
+ if verbose: print('LOGGING THIS EVENT')
fout = file(TOPIP_LOG_FILE,'a')
#dts = time.strftime('%Y:%m:%d:%H:%M:%S', time.localtime())
dts = time.asctime()
@@ -283,7 +287,7 @@ def main():
# save state to TOPIP_LAST_RUN_STATS
try:
pickle.dump(s, file(TOPIP_LAST_RUN_STATS,'w'))
- os.chmod (TOPIP_LAST_RUN_STATS, 0664)
+ os.chmod (TOPIP_LAST_RUN_STATS, 0o664)
except:
pass
# p.logout()
@@ -292,10 +296,10 @@ if __name__ == '__main__':
try:
main()
sys.exit(0)
- except SystemExit, e:
+ except SystemExit as e:
raise e
- except Exception, e:
- print str(e)
+ except Exception as e:
+ print(str(e))
traceback.print_exc()
os._exit(1)