/
ZiviosAgent.py
150 lines (128 loc) · 4.79 KB
/
ZiviosAgent.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
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
#python
"""
* Copyright (c) 2008 Zivios, LLC.
*
* This file is part of Zivios.
*
* Zivios 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 3 of the License, or
* (at your option) any later version.
*
* Zivios 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
* along with Zivios. If not, see <http://www.gnu.org/licenses/>.
*
* @package ZiviosAgent
* @copyright Copyright (c) 2008 Zivios, LLC. (http://www.zivios.org)
* @license http://www.zivios.org/legal/license
* @subpackage Core
"""
import xmlrpclib
from twisted.internet.threads import deferToThread
from twisted.web import xmlrpc, server, http
from twisted.python import log
from twisted.internet import defer, protocol, reactor
import ldap
import logging
import os
import os.path
import popen2
import base64
import re
import select
import pty
import sys
from ConfigParser import *
class ZiviosAgent(xmlrpc.XMLRPC):
configuration = ''
distrodef = ''
def __init__(self,distrodef):
pluginname = self.__class__.__name__
log.msg(pluginname+" Plugin Registered")
self.distrodef = distrodef
configdict = SafeConfigParser()
configdict.read('config/'+pluginname+'.ini')
self.configuration = configdict
def isPidRunning(self,pid):
intpid = int(pid)
log.msg("testing pid "+pid+" for running status")
try:
os.kill(intpid,0)
return 1
except OSError:
return 0
def checkProcess(self,pidfile):
if (os.path.exists(pidfile)):
fileHandle = open(pidfile)
data = fileHandle.read()
log.msg("read pid :"+data+" from pidfile :"+pidfile)
return self.isPidRunning(data.strip())
else:
return 0
def commandOnShell(self,popen4obj,command,endonregex):
regex = re.compile(endonregex)
outfile = popen4obj.fromchild
errfile = popen4obj.childerr
outfd = outfile.fileno()
errfd = errfile.fileno()
infile = popen4obj.tochild
infile.write(command)
infile.flush()
self.makeNonBlocking(outfd) # don't deadlock
self.makeNonBlocking(errfd)
outdata = errdata = ''
outeof = erreof = 0
while 1:
ready = select.select([outfd,errfd],[],[]) # wait for input
if outfd in ready[0]:
log.msg('attempting to read')
outchunk = outfile.read()
if outchunk == '': outeof = 1
outdata = outdata + outchunk
end = regex.search(outdata)
log.msg('read :'+outchunk)
if errfd in ready[0]:
errchunk = errfile.read()
if errchunk == '': erreof = 1
errdata = errdata + errchunk
log.msg('read err:'+errchunk)
if outeof : break
if end : break
select.select([],[],[],.1) # give a little time for buffers to fill
#if err != 0:
#raise RuntimeError, '%s failed w/ exit code %d\n%s' % (command, err, errdata)
return outdata
def getProperty(self,name,dictionary=None):
return self.configuration.get(self.distrodef,name,0,dictionary)
def getGeneralProperty(self,name,dictionary=None):
return self.configuration.get('general',name,0,dictionary)
"""
command needs the command name which it will extract from the module
config file and a dictionary which needs to be expanded
"""
def command(self,cmdname,dictionary=None,baseenc=0):
cmd = self.getProperty(cmdname,dictionary)
cmdcheckregex = self.getProperty(cmdname+".success",dictionary)
regex = re.compile(cmdcheckregex)
log.msg("running command :"+cmd+" and regex success match is :"+cmdcheckregex+"\n")
popen = popen2.Popen4(cmd)
filehandle = popen.fromchild
exitcode = popen.wait()
resp = filehandle.read()
filehandle.close()
popen.tochild.close()
success = regex.search(resp)
if (success):
regexcode = 1
else:
regexcode = 0
log.msg("Command returned: "+resp+" | regex response "+str(regexcode)+" | exitcode "+str(exitcode))
if (baseenc):
return base64.encodestring(resp),regexcode,exitcode
else:
return resp,regexcode,exitcode