|
@ -1,24 +1,12 @@ |
|
|
# (Copyright) 2015 ABF OSIELL <http://osiell.com> |
|
|
# (Copyright) 2015 ABF OSIELL <http://osiell.com> |
|
|
# (Copyright) 2018 Creu Blanca |
|
|
# (Copyright) 2018 Creu Blanca |
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
|
|
|
|
|
|
|
|
import logging |
|
|
import logging |
|
|
import os |
|
|
|
|
|
import shlex |
|
|
|
|
|
import subprocess |
|
|
|
|
|
|
|
|
|
|
|
from odoo import _, api, fields, models |
|
|
from odoo import _, api, fields, models |
|
|
from odoo.exceptions import UserError |
|
|
|
|
|
from odoo.tools.safe_eval import safe_eval |
|
|
from odoo.tools.safe_eval import safe_eval |
|
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__) |
|
|
_logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
SEND_NSCA_BIN = '/usr/sbin/send_nsca' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_exe(fpath): |
|
|
|
|
|
return os.path.isfile(fpath) and os.access(fpath, os.X_OK) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NscaCheck(models.Model): |
|
|
class NscaCheck(models.Model): |
|
|
_name = "nsca.check" |
|
|
_name = "nsca.check" |
|
@ -88,7 +76,7 @@ class NscaCheck(models.Model): |
|
|
|
|
|
|
|
|
@api.model |
|
|
@api.model |
|
|
def _cron_check(self, check_id): |
|
|
def _cron_check(self, check_id): |
|
|
self._check_send_nsca_command() |
|
|
|
|
|
|
|
|
self.env['nsca.server']._check_send_nsca_command() |
|
|
check = self.browse(check_id) |
|
|
check = self.browse(check_id) |
|
|
rc, message, performance = 3, "Unknown", {} |
|
|
rc, message, performance = 3, "Unknown", {} |
|
|
try: |
|
|
try: |
|
@ -116,63 +104,5 @@ class NscaCheck(models.Model): |
|
|
|
|
|
|
|
|
@api.multi |
|
|
@api.multi |
|
|
def _send_nsca(self, rc, message, performance): |
|
|
def _send_nsca(self, rc, message, performance): |
|
|
"""Send the result of the check to the NSCA daemon.""" |
|
|
|
|
|
for check in self: |
|
|
for check in self: |
|
|
msg = message |
|
|
|
|
|
if len(performance) > 0: |
|
|
|
|
|
msg += '| ' + ''.join( |
|
|
|
|
|
["%s=%s%s;%s;%s;%s;%s" % ( |
|
|
|
|
|
key, |
|
|
|
|
|
performance[key]['value'], |
|
|
|
|
|
performance[key].get('uom', ''), |
|
|
|
|
|
performance[key].get('warn', ''), |
|
|
|
|
|
performance[key].get('crit', ''), |
|
|
|
|
|
performance[key].get('min', ''), |
|
|
|
|
|
performance[key].get('max', ''), |
|
|
|
|
|
) for key in sorted(performance)]) |
|
|
|
|
|
check_result = self._format_check_result(check, rc, msg) |
|
|
|
|
|
cmd = self._prepare_command(check) |
|
|
|
|
|
self._run_command(check, cmd, check_result) |
|
|
|
|
|
|
|
|
|
|
|
@api.model |
|
|
|
|
|
def _format_check_result(self, check, rc, message): |
|
|
|
|
|
"""Format the check result with tabulations as delimiter.""" |
|
|
|
|
|
message = message.replace('\t', ' ') |
|
|
|
|
|
hostname = check.server_id.node_hostname |
|
|
|
|
|
check_result = u"%s\t%s\t%s\t%s" % ( |
|
|
|
|
|
hostname, check.service, rc, message) |
|
|
|
|
|
return check_result.encode('utf-8') |
|
|
|
|
|
|
|
|
|
|
|
@api.model |
|
|
|
|
|
def _prepare_command(self, check): |
|
|
|
|
|
"""Prepare the shell command used to send the check result |
|
|
|
|
|
to the NSCA daemon. |
|
|
|
|
|
""" |
|
|
|
|
|
cmd = u"/usr/sbin/send_nsca -H %s -p %s -c %s" % ( |
|
|
|
|
|
check.server_id.name, |
|
|
|
|
|
check.server_id.port, |
|
|
|
|
|
check.server_id.config_file_path) |
|
|
|
|
|
return shlex.split(cmd) |
|
|
|
|
|
|
|
|
|
|
|
@api.model |
|
|
|
|
|
def _run_command(self, check, cmd, check_result): |
|
|
|
|
|
"""Send the check result through the '/usr/sbin/send_nsca' command.""" |
|
|
|
|
|
try: |
|
|
|
|
|
proc = subprocess.Popen( |
|
|
|
|
|
cmd, |
|
|
|
|
|
stdout=subprocess.PIPE, |
|
|
|
|
|
stdin=subprocess.PIPE, |
|
|
|
|
|
stderr=subprocess.STDOUT) |
|
|
|
|
|
stdout = proc.communicate( |
|
|
|
|
|
input=check_result)[0] |
|
|
|
|
|
_logger.debug("%s: %s", check_result, stdout.strip()) |
|
|
|
|
|
except Exception as exc: |
|
|
|
|
|
_logger.error(exc) |
|
|
|
|
|
|
|
|
|
|
|
def _check_send_nsca_command(self): |
|
|
|
|
|
"""Check if the NSCA client is installed.""" |
|
|
|
|
|
if not is_exe(SEND_NSCA_BIN): |
|
|
|
|
|
raise UserError( |
|
|
|
|
|
_(u"Command '%s' not found. Please install the NSCA client.\n" |
|
|
|
|
|
u"On Debian/Ubuntu: apt-get install nsca-client") % ( |
|
|
|
|
|
SEND_NSCA_BIN)) |
|
|
|
|
|
|
|
|
check.server_id._send_nsca(check.service, rc, message, performance) |