Beispiel #1
0
 def _run_cmd(self, target, func_name, func_opts, cmd):
     check, _ = models.Check.objects.get_or_create(
         target=target, function=func_name,
         name=func_opts.get('name', func_name))
     self.stdout.write("+ {}".format(cmd))
     timestamp = timezone.now()
     result = subprocess.check_output(cmd, shell=True)
     check.last_run = timestamp
     check.active = True
     self.checked.append(check.pk)
     check.save()
     try:
         parsed = json.loads(result)
     except ValueError:
         self.stdout.write("  Error parsing results")
         return
     # parse out minion names in the event of a wildcard target
     for name, raw_value in parsed.iteritems():
         print("DEBUG: %s,%s" %(name, raw_value))
         value = utils.parse_value(raw_value, func_opts)
         self.stdout.write("  {}: {}".format(name, value))
         minion, _ = models.Minion.objects.get_or_create(name=name)
         failed = utils.check_failed(value, func_opts)
         models.Result.objects.create(timestamp=timestamp,
                                      result=value,
                                      result_type=func_opts['type'],
                                      check=check,
                                      minion=minion,
                                      failed=failed)
Beispiel #2
0
 def handle_noargs(self, **options):
     config = self.load_salmon_checks()
     self.stdout.write("Running checks...")
     for target, functions in config.items():
         for func_name, func_opts in functions.items():
             cmd = utils.build_command(target, func_name)
             check, _ = models.Check.objects.get_or_create(
                 target=target, function=func_name,
                 name=func_opts.get('name', func_name))
             self.stdout.write("+ {}".format(' '.join(cmd)))
             timestamp = timezone.now()
             result = subprocess.check_output(cmd)
             check.last_run = timestamp
             check.active = True
             check.save()
             try:
                 parsed = json.loads(result)
             except ValueError:
                 self.stdout.write("  Error parsing results")
                 continue
             # parse out minion names in the event of a wildcard target
             for name, raw_value in parsed.iteritems():
                 value = utils.parse_value(raw_value, func_opts)
                 self.stdout.write("  {}: {}".format(name, value))
                 minion, _ = models.Minion.objects.get_or_create(name=name)
                 failed = utils.check_failed(value, func_opts)
                 models.Result.objects.create(timestamp=timestamp,
                                              result=value,
                                              result_type=func_opts['type'],
                                              check=check,
                                              minion=minion,
                                              failed=failed)
Beispiel #3
0
    def _run_cmd(self, target, func_name, func_opts, cmd):
        """
        Runs salt checks and stores results to database.
        """
        check, _ = models.Check.objects.get_or_create(
            target=target, function=func_name,
            name=func_opts.get('name', func_name),
            alert_emails=",".join(func_opts.get('alert_emails', [])))
        self.stdout.write("+ {0}".format(cmd))
        timestamp = timezone.now()
        # shell out to salt command
        result = subprocess.Popen(cmd, shell=True,
            stdout=subprocess.PIPE).communicate()[0]

        if not check.active:
            check.active = True
            check.save()
        self.active_checks.append(check.pk)

        try:
            parsed = json.loads(result)
        except ValueError as err:
            self.stdout.write("  Error parsing results: %s" % err)
            return

        # parse out minion names in the event of a wildcard target
        for name, raw_value in parsed.iteritems():
            if int(self.options["verbosity"]) > 1:
                self.stdout.write(
                    "+     name: {0} -- str(raw_value): {1}".format(
                        name, str(raw_value)))
            value = utils.parse_value(raw_value, func_opts)
            self.stdout.write("   {0}: {1}".format(name, value))
            minion, _ = models.Minion.objects.get_or_create(name=name)
            failed = utils.check_failed(value, func_opts)
            self.stdout.write("   {0}: {1}".format("Assertion has failed",
                                                 failed))
            models.Result.objects.create(timestamp=timestamp,
                                         result=value,
                                         result_type=func_opts['type'],
                                         check=check,
                                         minion=minion,
                                         failed=failed)
Beispiel #4
0
    def _handle_result(self, target, func_name, func_opts, result):
        """
        handle salt check result and stores results to database.
        """
        # TODO: Move this elsewhere were it is more easily testable.
        alert_emails = func_opts.get('alert_emails', [])

        if alert_emails in [False, None]:
            alert_emails = models.Check.NO_EMAIL_FLAG
        else:
            alert_emails = ",".join(alert_emails)

        check, _ = models.Check.objects.get_or_create(
            target=target, function=func_name,
            name=func_opts.get('name', func_name),
            alert_emails=alert_emails)
        timestamp = timezone.now()

        if not check.active:
            check.active = True
            check.save()
        self.active_checks.append(check.pk)

        # parse out minion names in the event of a wildcard target
        for name, raw_value in result.iteritems():
            if int(self.options["verbosity"]) > 1:
                self.stdout.write(
                    "+     name: {0} -- str(raw_value): {1}".format(
                        name, str(raw_value)))
            value = utils.parse_value(raw_value, func_opts)
            self.stdout.write("   {0}: {1}".format(name, value))
            minion, _ = models.Minion.objects.get_or_create(name=name)
            failed = utils.check_failed(value, func_opts)
            self.stdout.write("   {0}: {1}".format("Assertion has failed",
                                                   failed))
            models.Result.objects.create(timestamp=timestamp,
                                         result=value,
                                         result_type=func_opts['type'],
                                         check=check,
                                         minion=minion,
                                         failed=failed)
Beispiel #5
0
    def _handle_result(self, target, func_name, func_opts, result):
        """
        handle salt check result and stores results to database.
        """
        # TODO: Move this elsewhere were it is more easily testable.
        alert_emails = func_opts.get('alert_emails', [])

        if alert_emails in [False, None]:
            alert_emails = models.Check.NO_EMAIL_FLAG
        else:
            alert_emails = ",".join(alert_emails)

        check, _ = models.Check.objects.get_or_create(
            target=target,
            function=func_name,
            name=func_opts.get('name', func_name),
            alert_emails=alert_emails)
        timestamp = timezone.now()

        if not check.active:
            check.active = True
            check.save()
        self.active_checks.append(check.pk)

        # parse out minion names in the event of a wildcard target
        for name, raw_value in result.iteritems():
            if int(self.options["verbosity"]) > 1:
                self.stdout.write(
                    "+     name: {0} -- str(raw_value): {1}".format(
                        name, str(raw_value)))
            value = utils.parse_value(raw_value, func_opts)
            self.stdout.write("   {0}: {1}".format(name, value))
            minion, _ = models.Minion.objects.get_or_create(name=name)
            failed = utils.check_failed(value, func_opts)
            self.stdout.write("   Assertion: {0}".format(not failed))
            models.Result.objects.create(timestamp=timestamp,
                                         result=value,
                                         result_type=func_opts['type'],
                                         check=check,
                                         minion=minion,
                                         failed=failed)