-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_peer_status
More file actions
167 lines (125 loc) · 4.59 KB
/
Copy pathcheck_peer_status
File metadata and controls
167 lines (125 loc) · 4.59 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/python
import os, sys, socket, string
from optparse import *
"""
Check_peer_status plugin for Boundary. Copyright (c) 2014 Alex Frangis
Version 0.1.0 updated on 09/21/2014
Based on Check_peer_status plugin for Nagios Copyright (c) 2013 Andrea Zorzetto
Version 0.2.2 updated at 21/11/2013
The modifications I made are mere characters, Andrea did all the real work
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; either version 2 of the License, or
(at your option) any later version.
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.
"""
# Process the command line...
parser = OptionParser(usage="Check_peer_status [options]", version="%prog 0.1.0")
parser.set_defaults(hostname='127.0.0.1')
parser.set_defaults(port=5038)
parser.set_defaults(peer="")
parser.add_option("-u", "--username", action="store", dest="user",
help="username for AMI.")
parser.add_option("-s", "--secret", action="store", dest="secret",
help="password for AMI.")
parser.add_option("-H", "--host", action="store", dest="hostname",
help="the host to connect to. The default is localhost.")
parser.add_option("-P", "--port", action="store", dest="port",
help="the port to contact. Default is 5038.")
parser.add_option("-t", "--type", action="store", dest="type",
help="sip or iax are allowed values.")
parser.add_option("-p", "--peer", action="store", dest="peer",
help="the peer name to check.")
parser.add_option("-a", "--all", action="store_true", dest="all",
help="print the whole output.")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
help="print the whole output.")
(options, args) = parser.parse_args()
# Define the socket connection
mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
login = """Action: login\r\nUsername: """ + options.user + """\r\nSecret: """ + options.secret + """\r\nEvents: off\r\n\r\n"""
if (options.type=="sip"):
command="sip show peer"
commandall="sip show peers"
elif (options.type=="iax"):
command="iax2 show peer"
commandall="iax2 show peers"
else:
print "Type peer error"+options.type
sys.exit(1)
if (options.all):
action = """Action: command\r\nCommand: """ + commandall + """\r\n\r\n"""
else:
action = """Action: command\r\nCommand: """ + command + """ """+ options.peer +"""\r\n\r\n"""
logout = """Action: logoff\r\n\r\n"""
#global port
host = options.hostname
port = int(options.port)
user = options.user
password = options.secret
def connect(host, user, password):
mysocket.connect((host, port))
mysocket.send(login)
def disconnect(logout):
send_command(logout)
mysocket.send(logout)
mysocket.close()
def send_command(action):
mysocket.send(action)
global myrcvd
myrcvd = ""
while 1:
data = mysocket.recv(4096) #The output bytes from the socket connection. You can adjust size to taste.
myrcvd = myrcvd + data
#print "$"+ data +"_"
#print len(data)
if (len(data)==0) or (string.find(data,'END COMMAND')>0):
break
return myrcvd
def get_peer_status(myrcvd):
#Search peer status
pos1= string.find(myrcvd,'Status')
pos2= string.find(myrcvd[pos1:],'\n')
#get peer status
status=myrcvd[pos1:]
status=status[:pos2]
return status
def extract(string, start='(', stop=')'):
return string[string.index(start)+1:string.index(stop)]
# Perform the operation...
try:
connect(host, user, password)
result=send_command(action)
disconnect(logout)
#check auth
if string.find(result,'accepted') != -1:
if (options.all):
print result
elif (options.verbose):
print result
else:
status=get_peer_status(result)
if (string.find(status,'OK') >0):
latency = extract(status)
print "ASTERISK_PEER_RESPONSE_TIME "+latency
elif (string.find(status,'LAGGED') >0):
latency = extract(status)
print "ASTERISK_PEER_RESPONSE_TIME "+latency
elif (string.find(status,'UNKNOWN') >0):
latency = extract(status)
print "ASTERISK_PEER_RESPONSE_TIME "+latency
elif (string.find(status,'unmonitored') >0):
print "ASTERISK_PEER_RESPONSE_TIME "
else:
print status
sys.exit(2)
else:
print "Critical - Authentication failed"
sys.exit(2)
sys.exit(0)
except socket.error:
print "Critical - Cannot contact Asterisk!"
sys.exit(2)