Skip to content

Example of using ncm-ncd hooks to provide status reports back to profile server #140

@jrha

Description

@jrha

As discussed briefly at today's catch-up.

Perhaps for inclusion in the repo as examples?
Maybe a basis for something more scalable and robust?

/usr/sbin/aq_reporter_hook

#!/bin/bash
echo '[HOOK] Sending report to aquilon.example.org'
curl -s --capath /etc/grid-security/certificates https://aquilon.example.org/cgi-bin/ncm-ncd-callback --data @- && exit 0
exit 0

/etc/ncm-ncd.conf

post-hook = /usr/sbin/aq_reporter_hook
post-hook-timeout = 10
pre-hook = /usr/sbin/aq_reporter_hook
pre-hook-timeout = 10

/var/www/cgi-bin/ncm-ncd-callback

#!/usr/bin/env python2

print "Content-Type: text/plain"
print

from os import environ
import cgi
from datetime import datetime
from json import loads
from re import compile as re_compile, IGNORECASE
from sys import stdin


def is_valid_hostname(hostname):
    if len(hostname) > 255:
        return False
    if hostname[-1] == ".":
        hostname = hostname[:-1] # strip exactly one dot from the right, if present
    allowed = re_compile("(?!-)[A-Z\d-]{1,63}(?<!-)$", IGNORECASE)
    return all(allowed.match(x) for x in hostname.split("."))


def main():
	hostname = ''
	if 'REMOTE_HOST' in environ:
	    hostname = environ["REMOTE_HOST"]

	response = ''
	for line in stdin:
	    response += line.strip() + "\n"

        response_valid = False
        try:
            r = loads(response)
            if len(r) <= 5:
                response_valid = True
        except ValueError:
            pass

	if response and response_valid and hostname and is_valid_hostname(hostname):
	    report_file = open('/var/quattor/host_status/%s' % hostname, 'w')
	    report_file.write(response)
	    report_file.write("\n")
	    report_file.close()
	    print "[ AQ ] Report accepted for %s" % (hostname)
	else:
	    print "[ AQ ] Invalid report"


main()

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions