def send_nsca( self, code, message, nscahost, hostname=node(), service=None, nscabin="send_nsca", nscaconf=None ): """ Send data via send_nsca for passive service checks Arguments: code -- Return code of plugin. message -- Message to pass back. nscahost -- Hostname or IP address of NSCA server. hostname -- Hostname the check results apply to. service -- Service the check results apply to. nscabin -- Location of send_nsca binary. If none specified whatever is in the path will be used. nscaconf -- Location of the NSCA configuration to use if any. """ # Format nscaconf if necessary if nscaconf is not None: nscaconf = "-c %s" % nscaconf # Build command command = "%s %s -H %s" % (nscabin, nscaconf or "", nscahost) # Service check if service: command = "echo -e '%s\t%s\t%d\t%s' | %s" % ( hostname, service, code, message, command ) # Host check, omit service_description else: command = "echo -e '%s\t%d\t%s' | %s" % ( hostname, code, message, command ) # Execute command runCommand(command, raise_error_on_fail=True) return 0
def run_query(): """ Connect to a remote storwize box and run query """ command = "ssh %s@%s %s -delim ':'" % (p.options.user, p.options.hostname, p.options.query) if p.options.test: command = "cat %s.txt" % (p.options.query) return_code, stdout, stderr = runCommand(command) if return_code != 0: p.status(unknown) p.add_summary( "Got error %s when trying to log into remote storwize box" % return_code) p.add_long_output("\ncommand:\n===============\n%s" % command) p.add_long_output("\nStandard output:\n==============\n%s" % (stdout)) p.add_long_output("\nStandard stderr:\n==============\n%s" % (stderr)) p.exit() if stderr: p.status(unknown) p.add_summary("Error when connecting to storwize: %s" % stderr) p.exit() # Parse the output of run query and return a list of "rows" lines = stdout.splitlines() top_line = lines.pop(0) headers = top_line.split(':') Row = namedtuple('Row', ' '.join(headers)) rows = [] for i in lines: i = i.strip() columns = i.split(':') row = Row(*columns) rows.append(row) return rows
def run_query(): """ Connect to a remote storwize box and run query """ command = "ssh %s@%s %s -delim ':'" % (p.options.user, p.options.hostname, p.options.query) if p.options.test: command = "cat %s.txt" % (p.options.query) return_code, stdout, stderr = runCommand(command) if return_code != 0: p.status(unknown) p.add_summary("Got error %s when trying to log into remote storwize box" % return_code) p.add_long_output("\ncommand:\n===============\n%s" % command) p.add_long_output("\nStandard output:\n==============\n%s" % (stdout)) p.add_long_output("\nStandard stderr:\n==============\n%s" % (stderr)) p.exit() if stderr: p.status(unknown) p.add_summary("Error when connecting to storwize: %s" % stderr) p.exit() # Parse the output of run query and return a list of "rows" lines = stdout.splitlines() top_line = lines.pop(0) headers = top_line.split(':') Row = namedtuple('Row', ' '.join(headers)) rows = [] for i in lines: i = i.strip() columns = i.split(':') row = Row(*columns) rows.append(row) return rows
def verify_config(self): """ Run nagios -v config_file to verify that the conf is working :returns: True -- if pynag.Utils.runCommand() returns 0, else None """ cmd = [self.nagios_bin, "-v", self.nagios_cfg] if self.sudo: cmd = ['sudo', '-n'] + cmd result, self.stdout, self.stderr = runCommand(cmd, shell=False) if result == 0: return True else: return None
def reload(self): """ Reloads Nagios. :returns: Return code of the reload command ran by pynag.Utils.runCommand() :rtype: int """ if self.method == daemon.SYSV_INIT_SCRIPT: cmd = [self.nagios_init, "reload"] else: cmd = ["service", self.service_name, "reload"] if self.sudo: cmd = ['sudo', '-n'] + cmd result, self.stdout, self.stderr = runCommand(cmd, shell=False) return result
def status(self): """ Obtain the status of the Nagios service. :returns: Return code of the status command ran by pynag.Utils.runCommand() :rtype: int """ if self.method == daemon.SYSV_INIT_SCRIPT: cmd = [self.nagios_init, "status"] else: cmd = ["service", self.service_name, "status"] if self.sudo: cmd = ['sudo', '-n'] + cmd result, self.stdout, self.stderr = runCommand(cmd, shell=False) return result
def stop(self): """ Stop the Nagios service. :returns: Return code of the stop command ran by pynag.Utils.runCommand() :rtype: int """ if self.method == daemon.SYSV_INIT_SCRIPT: cmd = [self.nagios_init, "stop"] else: cmd = ["service", self.service_name, "stop"] if self.sudo: cmd.insert(0, 'sudo') result, self.stdout, self.stderr = runCommand(cmd, shell=False) return result
def restart(self): """ Restarts Nagios via it's init script. :returns: Return code of the restart command ran by pynag.Utils.runCommand() :rtype: int """ if self.method == daemon.SYSV_INIT_SCRIPT: cmd = [self.nagios_init, "restart"] else: cmd = ["service", self.service_name, "restart"] if self.sudo: cmd.insert(0, 'sudo') result, self.stdout, self.stderr = runCommand(cmd, shell=False) return result
def running(self): """ Checks if the daemon is running :returns: Whether or not the daemon is running :rtype: bool """ if self.method == daemon.SYSV_INIT_SCRIPT or \ self.method == daemon.SYSV_INIT_SERVICE: if self.nagios_config is None: self.nagios_config = pynag.Parsers.config() if self.nagios_config._get_pid(): return True elif self.method == daemon.SYSTEMD: result = runCommand(["systemctl", "is-active", self.service_name], shell=False) if result[0] == 0: return True return False