Commit 22af6c0c authored by Olav Morken's avatar Olav Morken
Browse files

Add option to send result to a Zabbix item.

parent 86fff76d
......@@ -15,6 +15,8 @@ Features
* Timeout on execution (default 60 seconds).
* Send result of operation to a Zabbix item.
Examples
--------
......@@ -53,3 +55,15 @@ run_docker.py --name name-of-the-job --device /dev/net/tun --cap-add NET_ADMIN
```
run_docker.py --name name-of-the-job --timeout=30
```
### Report status to Zabbix
This reports the result of the job to our Zabbix server.
It is added to the item with key "someitem" on the host "host.example.org".
"someitem" must be a "Zabbix trapper" item, with type "Numeric (unsigned)".
The value is reported as "1" on success and "0" on failure.
```
run_docker.py --name name-of-the-job --zabbix-item=host.example.org:someitem
```
......@@ -244,6 +244,33 @@ class _DockerRunner(object):
# Remove the container name, to mark the container as not running.
self.container_name = None
def _parse_zabbix_item(item):
try:
host, key = item.split(':', 1)
except:
raise argparse.ArgumentTypeError('Zabbix-item must be on the form HOST:ITEM')
return (host, key)
def _send_zabbix_result(zabbix_item, ok):
if not zabbix_item:
return
if ok:
value = 1
else:
value = 0
hostname, key = zabbix_item
args = [
'zabbix_sender',
'--zabbix-server', 'zabbix-backend.uninett.no',
'--host', hostname,
'--key', key,
'--value', str(value),
'-vv',
]
subprocess.check_call(args)
parser = argparse.ArgumentParser()
parser.add_argument('--name', required=True, help="Name of task to run. Mainly used to identify the docker image.")
......@@ -253,10 +280,16 @@ parser.add_argument('--env', action='append', default=[], help="Pass-through for
parser.add_argument('--device', action='append', default=[], help="Pass-through for Docker --device option.")
parser.add_argument('--cap-add', action='append', default=[], help="Pass-through for Docker --cap-add option.")
parser.add_argument('--volume', action='append', default=[], help="Pass-through for Docker --volume option.")
parser.add_argument('--zabbix-item', type=_parse_zabbix_item, metavar='HOST:KEY', help="Zabbix item to report status to.")
parser.add_argument('cmd', nargs='*', help="Command to run in the docker container.")
args = parser.parse_args()
runner = _DockerRunner(args)
runner.run()
sys.exit(runner.retcode)
try:
runner = _DockerRunner(args)
runner.run()
_send_zabbix_result(args.zabbix_item, runner.retcode == 0)
sys.exit(runner.retcode)
except:
_send_zabbix_result(args.zabbix_item, False)
raise
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment