-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyclient.py
More file actions
123 lines (101 loc) · 4.12 KB
/
Copy pathmyclient.py
File metadata and controls
123 lines (101 loc) · 4.12 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
#!/usr/bin/env python
#------------------------------------------------------------
# FILE: myclient.py
# PURPOSE:
#
# AUTHOR: Jason G Yates
# DATE: 5-Apr-2017
# MODIFICATIONS:
#------------------------------------------------------------
import datetime, time, sys, smtplib, signal, os, threading, socket
import mylog
#---------- ClientInterface::init--- ------------------------------------------
class ClientInterface:
def __init__(self, host="127.0.0.1", port=9082, log = None):
if log != None:
self.log = log
else:
# log errors in this module to a file
self.log = mylog.SetupLogger("client", "/var/log/myclient.log")
self.EndOfMessage = "EndOfMessage"
self.rxdatasize = 2000
self.host = host
self.port = port
self.Connect()
#---------- ClientInterface::Connect ---------------------------------
def Connect(self):
try:
#create an INET, STREAMing socket
self.Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#now connect to the server on our port
self.Socket.connect((self.host, self.port))
sRetData, data = self.Receive(noeom = True) # Get initial status before commands are sent
print(data)
except Exception as e1:
self.FatalError("Error: Connect" + str(e1))
#---------- ClientInterface::SendCommand ---------------------------------
def SendCommand(self, cmd):
try:
self.Socket.sendall(cmd.encode())
except Exception as e1:
self.LogError( "Error: TX:" + str(e1))
self.Close()
self.Connect()
#---------- ClientInterface::Receive ---------------------------------
def Receive(self, noeom = False):
RetStatus = True
try:
bytedata = self.Socket.recv(self.rxdatasize)
data = bytedata.decode()
if len(data):
if not self.CheckForStarupMessage(data) or not noeom:
while not self.EndOfMessage in data:
morebytes = self.Socket.recv(self.rxdatasize)
more = morebytes.decode()
if len(more):
if self.CheckForStarupMessage(more):
data = ""
RetStatus = False
break
data += more
if data.endswith(self.EndOfMessage):
data = data[:-len(self.EndOfMessage)]
RetStatus = True
else:
self.Connect()
return False, data
except Exception as e1:
self.LogError( "Error: RX:" + str(e1))
self.Close()
self.Connect()
RetStatus = False
data = "Retry"
return RetStatus, data
#---------- ClientInterface::CheckForStarupMessage ---------------------------------
def CheckForStarupMessage(self, data):
# check for initial status response from monitor
if data.startswith("OK") or data.startswith("CRITICAL:") or data.startswith("WARNING:"):
return True
else:
return False
#---------- ClientInterface::Close ---------------------------------
def Close(self):
self.Socket.close()
#---------- ClientInterface::ProcessMonitorCommand ---------------------------------
def ProcessMonitorCommand(self, cmd):
data = ""
try:
RetStatus = False
while RetStatus == False:
self.SendCommand(cmd)
RetStatus, data = self.Receive()
except Exception as e1:
self.LogError("Error in ProcessMonitorCommand:" + str(e1))
return data
#---------------------ClientInterface::FatalError------------------------
def LogError(self, Message):
self.log.error(Message)
#---------- ClientInterface::FatalError ---------------------------------
def FatalError(self, Message):
self.log.error(Message)
raise Exception(Message)