-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifupdownevent.py
executable file
·92 lines (71 loc) · 2.14 KB
/
ifupdownevent.py
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
#!/usr/bin/python
#
# Network interface up/down event generator
# Call with check argument to generete an event for each interface
#
# Utils
from time import sleep
import sys
#sys.path.append('../modules')
sys.path.append('/mnt/PIHU_APP/defender-headunit/modules')
from hu_utils import *
from modules.hu_msg import MqPubSubFwdController
DESCRIPTION = "Send a MQ event"
DEFAULT_PORT_SUB = 5560
DEFAULT_PORT_PUB = 5559
args = None
messaging = None
#********************************************************************************
# Parse command line arguments
#
def parse_args():
import argparse
global args
parser = argparse.ArgumentParser(description=DESCRIPTION)
parser.add_argument('-v', action='store_true', help='Verbose')
parser.add_argument('-i', action='store_true', help='Check if internet available')
parser.add_argument('--port_publisher', action='store')
parser.add_argument('--port_subscriber', action='store')
parser.add_argument('ifupdown', action='store', choices=['ifup','ifdown','check']) #, required=True ?
#parser.add_argument('interface', action='store')
args = parser.parse_args()
#********************************************************************************
# Setup
#
def setup():
#
# ZMQ
#
global messaging
messaging = MqPubSubFwdController('localhost',DEFAULT_PORT_PUB,DEFAULT_PORT_SUB)
messaging.create_publisher()
messaging.create_subscriber()
sleep(1) #required?
def main():
cmd = 'DATA'
state = None
params = []
if args.ifupdown == 'ifup':
path = '/events/network/up'
state = "up"
elif args.ifupdown == 'ifdown':
path = '/events/network/down'
state = "down"
elif args.ifupdown == 'check':
print "check not supported"
exit(0)
internet_available = internet()
#if args.interface is not None:
# params.append('"interface":"{0}"'.format(args.interface))
if state is not None:
params.append('"state":"{0}"'.format(state))
if internet_available is not None:
params.append('"internet":"{0}"'.format(internet_available))
param = "{{{0}}}".format(','.join(params))
print param
ret = messaging.publish(path,cmd,param)
print ret
if __name__ == '__main__':
parse_args()
setup()
main()