Simplify agent args and correct parsing
This commit is contained in:
parent
02e50d6167
commit
318cdd5633
49
pgmon.py
49
pgmon.py
@ -178,8 +178,8 @@ class Request:
|
||||
"""
|
||||
A metric request
|
||||
"""
|
||||
def __init__(self, key, args = {}):
|
||||
self.key = key
|
||||
def __init__(self, metric_name, args = {}):
|
||||
self.metric_name = metric_name
|
||||
self.args = args
|
||||
self.result = None
|
||||
|
||||
@ -322,14 +322,14 @@ class Agent:
|
||||
The agent side of the connector
|
||||
"""
|
||||
@staticmethod
|
||||
def run(config, key, args):
|
||||
def run(config, key):
|
||||
# Connect to the socket
|
||||
ipc = IPC(config, 'agent')
|
||||
try:
|
||||
conn = ipc.connect()
|
||||
|
||||
# Send a request
|
||||
ipc.send(conn, "{},{}".format(key, args))
|
||||
ipc.send(conn, key)
|
||||
|
||||
# Wait for a response
|
||||
res = ipc.recv(conn)
|
||||
@ -379,26 +379,30 @@ class Server:
|
||||
continue
|
||||
|
||||
# Get the request string (csv)
|
||||
req_csv = ipc.recv(conn)
|
||||
key = ipc.recv(conn)
|
||||
|
||||
# Receive ipc request (csv)
|
||||
# Parse ipc request (csv)
|
||||
try:
|
||||
(key, args_str) = req_csv.split(',', 1)
|
||||
parts = key.split(',', 1)
|
||||
metric_name = parts[0]
|
||||
args_dict = {}
|
||||
if args_str is not None and args_str != "":
|
||||
for (k, v) in [a.split('=', 1) for a in args_str.split(';')]:
|
||||
|
||||
if len(parts) > 1:
|
||||
for arg in parts[1].split(','):
|
||||
(k, v) = arg.split('=', 1)
|
||||
args_dict[k] = v
|
||||
except socket.timeout:
|
||||
print("IPC communication timeout receiving request")
|
||||
conn.close()
|
||||
continue
|
||||
except Exception:
|
||||
print("Received invalid request: '{}'".format(req_csv))
|
||||
print("Received invalid request: '{}'".format(key))
|
||||
ipc.send(conn, "ERROR: Invalid key")
|
||||
conn.close()
|
||||
continue
|
||||
|
||||
# Create request object
|
||||
req = Request(key, args_dict)
|
||||
req = Request(metric_name, args_dict)
|
||||
|
||||
# Queue the request
|
||||
try:
|
||||
@ -447,9 +451,9 @@ class Worker(threading.Thread):
|
||||
|
||||
# Find the requested metrtic
|
||||
try:
|
||||
metric = self.config.metrics[req.key]
|
||||
metric = self.config.metrics[req.metric_name]
|
||||
except KeyError:
|
||||
req.set_result("ERROR: Unknown key '{}'".format(req.key))
|
||||
req.set_result("ERROR: Unknown key '{}'".format(req.metric_name))
|
||||
continue
|
||||
|
||||
# Get the DB version
|
||||
@ -528,21 +532,20 @@ def main():
|
||||
parser.add_argument('-v', '--verbose', action='store_true')
|
||||
|
||||
# Operational mode
|
||||
parser.add_argument('-m', '--mode', choices=['agent', 'server'], required=True)
|
||||
parser.add_argument('-s', '--server', action='store_true')
|
||||
|
||||
# Agent options
|
||||
parser.add_argument('-k', '--key')
|
||||
parser.add_argument('-a', '--args')
|
||||
parser.add_argument('key', nargs='?')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.mode == 'agent':
|
||||
config = Config(args.config, read_metrics = False)
|
||||
Agent.run(config, args.key, args.args)
|
||||
|
||||
else:
|
||||
if args.server:
|
||||
config = Config(args.config)
|
||||
Server.run(config)
|
||||
else:
|
||||
config = Config(args.config, read_metrics = False)
|
||||
Agent.run(config, args.key)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@ -560,13 +563,13 @@ class TestRequest:
|
||||
def test_request_creation(self):
|
||||
# Create result with no args
|
||||
req1 = Request('foo', {})
|
||||
assert req1.key == 'foo'
|
||||
assert req1.metric_name == 'foo'
|
||||
assert len(req1.args) == 0
|
||||
assert req1.complete.locked()
|
||||
|
||||
# Create result with args
|
||||
req2 = Request('foo', {'arg1': 'value1', 'arg2': 'value2'})
|
||||
assert req2.key == 'foo'
|
||||
assert req2.metric_name == 'foo'
|
||||
assert len(req2.args) == 2
|
||||
assert req2.complete.locked()
|
||||
|
||||
|
||||
@ -1 +1 @@
|
||||
UserParameter=pgmon[*],/usr/local/bin/pgmon.py -c /etc/zabbix/pgmon.cfg -k "$1" -a "$2"
|
||||
UserParameter=pgmon[*],/usr/local/bin/pgmon.py -c /etc/zabbix/pgmon.cfg "$0"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user