Exemplo n.º 1
0
  def run(self):
    """Executes the specified command."""
    self.nagios = Nagios()

    # expand host list, if needed
    self.hosts = self.hosts.split(',')
    if self.hosts[0][0] == '%':
      def addflag(flags, f, no, v):
        flags['!'+f] = flags[f] = v
        if no:
          flags.pop(f)
        else:
          flags.pop('!'+f)

      flags = {}
      exps = self.hosts
      exps[0] = exps[0][1:]
      for exp in exps:
        n = exp[0] == '!'
        if n: exp = exp[1:]
        if exp == 'all': flags = {}
        elif exp == 'ack': addflag(flags, 'problem_has_been_acknowledged', n, 1)
        elif exp in ('crit','critical'): addflag(flags, 'current_state', n, Nagios.STATE_CRITICAL)
        elif exp in ('warn','warning'): addflag(flags, 'current_state', n, Nagios.STATE_CRITICAL)
        elif exp == 'ok': addflag(flags, 'current_state', n, Nagios.STATE_OK)
        else:
          print 'E: unknown expanѕion "%s"' % exp
          return 2
      self.hosts = self.nagios.get_host_list_by_service(config.apt_service_name, flags)
    else:
      hosts = []
      for host in self.hosts:
        hosts.append(Host(host))
      self.hosts = hosts

    print "Operating on the following hosts:"
    if len(self.hosts) == 0:
      print "None. Nothing to do, exiting!"
      return 0
    print ", ".join(map(str, self.hosts))
    print

    if Main.debug:
      import paramiko.util
      import logging
      l = logging.getLogger('paramiko')
      l.setLevel(logging.DEBUG)
      lh = logging.StreamHandler(sys.stderr)
      lh.setFormatter(logging.Formatter('%(levelname)-.3s [%(asctime)s.%(msecs)03d] thr=%(_threadid)-3d %(name)s: %(message)s','%Y%m%d-%H:%M:%S'))
      l.addHandler(lh)

    # now really do something
    if self.action == 'recheck':
      return self.cmd_recheck()
    elif self.action == 'upgrade':
      return self.cmd_upgrade()
Exemplo n.º 2
0
    def run(self):
        """Executes the specified command."""
        self.nagios = Nagios()

        # expand host list, if needed
        self.hosts = self.hosts.split(',')
        if self.hosts[0][0] == '%':

            def addflag(flags, f, no, v):
                flags['!' + f] = flags[f] = v
                if no:
                    flags.pop(f)
                else:
                    flags.pop('!' + f)

            flags = {}
            exps = self.hosts
            exps[0] = exps[0][1:]
            for exp in exps:
                n = exp[0] == '!'
                if n: exp = exp[1:]
                if exp == 'all': flags = {}
                elif exp == 'ack':
                    addflag(flags, 'problem_has_been_acknowledged', n, 1)
                elif exp in ('crit', 'critical'):
                    addflag(flags, 'current_state', n, Nagios.STATE_CRITICAL)
                elif exp in ('warn', 'warning'):
                    addflag(flags, 'current_state', n, Nagios.STATE_CRITICAL)
                elif exp == 'ok':
                    addflag(flags, 'current_state', n, Nagios.STATE_OK)
                else:
                    print 'E: unknown expanѕion "%s"' % exp
                    return 2
            self.hosts = self.nagios.get_host_list_by_service(
                config.apt_service_name, flags)
        else:
            hosts = []
            for host in self.hosts:
                hosts.append(Host(host))
            self.hosts = hosts

        print "Operating on the following hosts:"
        if len(self.hosts) == 0:
            print "None. Nothing to do, exiting!"
            return 0
        print ", ".join(map(str, self.hosts))
        print

        if Main.debug:
            import paramiko.util
            import logging
            l = logging.getLogger('paramiko')
            l.setLevel(logging.DEBUG)
            lh = logging.StreamHandler(sys.stderr)
            lh.setFormatter(
                logging.Formatter(
                    '%(levelname)-.3s [%(asctime)s.%(msecs)03d] thr=%(_threadid)-3d %(name)s: %(message)s',
                    '%Y%m%d-%H:%M:%S'))
            l.addHandler(lh)

        # now really do something
        if self.action == 'recheck':
            return self.cmd_recheck()
        elif self.action == 'upgrade':
            return self.cmd_upgrade()
Exemplo n.º 3
0
class Main:
    """Contains the actual glue."""
    def __init__(self, argv):
        self.argv = argv
        ### Unbuffered sys.stdout
        sys.stdout = os.fdopen(1, 'w', 0)

    @staticmethod
    def main(argv):
        """Application entry point."""
        App = Main(argv)
        rc = App.parse_args()
        if rc > 0: return rc
        return App.run()

    def run(self):
        """Executes the specified command."""
        self.nagios = Nagios()

        # expand host list, if needed
        self.hosts = self.hosts.split(',')
        if self.hosts[0][0] == '%':

            def addflag(flags, f, no, v):
                flags['!' + f] = flags[f] = v
                if no:
                    flags.pop(f)
                else:
                    flags.pop('!' + f)

            flags = {}
            exps = self.hosts
            exps[0] = exps[0][1:]
            for exp in exps:
                n = exp[0] == '!'
                if n: exp = exp[1:]
                if exp == 'all': flags = {}
                elif exp == 'ack':
                    addflag(flags, 'problem_has_been_acknowledged', n, 1)
                elif exp in ('crit', 'critical'):
                    addflag(flags, 'current_state', n, Nagios.STATE_CRITICAL)
                elif exp in ('warn', 'warning'):
                    addflag(flags, 'current_state', n, Nagios.STATE_CRITICAL)
                elif exp == 'ok':
                    addflag(flags, 'current_state', n, Nagios.STATE_OK)
                else:
                    print 'E: unknown expanѕion "%s"' % exp
                    return 2
            self.hosts = self.nagios.get_host_list_by_service(
                config.apt_service_name, flags)
        else:
            hosts = []
            for host in self.hosts:
                hosts.append(Host(host))
            self.hosts = hosts

        print "Operating on the following hosts:"
        if len(self.hosts) == 0:
            print "None. Nothing to do, exiting!"
            return 0
        print ", ".join(map(str, self.hosts))
        print

        if Main.debug:
            import paramiko.util
            import logging
            l = logging.getLogger('paramiko')
            l.setLevel(logging.DEBUG)
            lh = logging.StreamHandler(sys.stderr)
            lh.setFormatter(
                logging.Formatter(
                    '%(levelname)-.3s [%(asctime)s.%(msecs)03d] thr=%(_threadid)-3d %(name)s: %(message)s',
                    '%Y%m%d-%H:%M:%S'))
            l.addHandler(lh)

        # now really do something
        if self.action == 'recheck':
            return self.cmd_recheck()
        elif self.action == 'upgrade':
            return self.cmd_upgrade()

    def parse_args(self):
        """Parse command and options from command line."""
        self.program_name = self.argv[0]

        if len(self.argv[1:]) == 0:
            return self.show_usage()

        self.action = self.argv[1]
        if self.action not in ['recheck', 'upgrade']:
            print "E: command \"%s\" not recognized." % self.action
            return 2

        try:
            opts, args = getopt.getopt(self.argv[2:], "nhdH:",
                                       ["dry-run", "help", "hosts="])
        except getopt.GetoptError, err:
            print "E: %s" % str(
                err)  # will print something like "option -a not recognized"
            return 2

        self.hosts = config.hosts_default
        Main.debug = False
        self.dry_run = False
        self.download_only = False
        for o, a in opts:
            if o in ('-h', '--help'):
                return self.show_usage()
            elif o in ('-d'):
                self.download_only = True
            elif o in ('-D'):
                print 'D: Enable debug.'
                Main.debug = True
            elif o in ('-n', '--dry-run'):
                self.dry_run = True
            elif o in ('-H', '--hosts'):
                self.hosts = a
Exemplo n.º 4
0
class Main:
  """Contains the actual glue."""

  def __init__(self, argv):
    self.argv = argv
    ### Unbuffered sys.stdout
    sys.stdout = os.fdopen(1, 'w', 0)

  @staticmethod
  def main(argv):
    """Application entry point."""
    App = Main(argv)
    rc = App.parse_args()
    if rc > 0: return rc
    return App.run()

  def run(self):
    """Executes the specified command."""
    self.nagios = Nagios()

    # expand host list, if needed
    self.hosts = self.hosts.split(',')
    if self.hosts[0][0] == '%':
      def addflag(flags, f, no, v):
        flags['!'+f] = flags[f] = v
        if no:
          flags.pop(f)
        else:
          flags.pop('!'+f)

      flags = {}
      exps = self.hosts
      exps[0] = exps[0][1:]
      for exp in exps:
        n = exp[0] == '!'
        if n: exp = exp[1:]
        if exp == 'all': flags = {}
        elif exp == 'ack': addflag(flags, 'problem_has_been_acknowledged', n, 1)
        elif exp in ('crit','critical'): addflag(flags, 'current_state', n, Nagios.STATE_CRITICAL)
        elif exp in ('warn','warning'): addflag(flags, 'current_state', n, Nagios.STATE_CRITICAL)
        elif exp == 'ok': addflag(flags, 'current_state', n, Nagios.STATE_OK)
        else:
          print 'E: unknown expanѕion "%s"' % exp
          return 2
      self.hosts = self.nagios.get_host_list_by_service(config.apt_service_name, flags)
    else:
      hosts = []
      for host in self.hosts:
        hosts.append(Host(host))
      self.hosts = hosts

    print "Operating on the following hosts:"
    if len(self.hosts) == 0:
      print "None. Nothing to do, exiting!"
      return 0
    print ", ".join(map(str, self.hosts))
    print

    if Main.debug:
      import paramiko.util
      import logging
      l = logging.getLogger('paramiko')
      l.setLevel(logging.DEBUG)
      lh = logging.StreamHandler(sys.stderr)
      lh.setFormatter(logging.Formatter('%(levelname)-.3s [%(asctime)s.%(msecs)03d] thr=%(_threadid)-3d %(name)s: %(message)s','%Y%m%d-%H:%M:%S'))
      l.addHandler(lh)

    # now really do something
    if self.action == 'recheck':
      return self.cmd_recheck()
    elif self.action == 'upgrade':
      return self.cmd_upgrade()

  def parse_args(self):
    """Parse command and options from command line."""
    self.program_name = self.argv[0]

    if len(self.argv[1:]) == 0:
      return self.show_usage()

    self.action = self.argv[1]
    if self.action not in ['recheck', 'upgrade']:
      print "E: command \"%s\" not recognized." % self.action
      return 2

    try:
      opts, args = getopt.getopt(self.argv[2:], "nhdH:", ["dry-run", "help", "hosts="])
    except getopt.GetoptError, err:
      print "E: %s" % str(err) # will print something like "option -a not recognized"
      return 2

    self.hosts = config.hosts_default
    Main.debug = False
    self.dry_run = False
    self.download_only = False
    for o, a in opts:
      if o in ('-h', '--help'):
        return self.show_usage()
      elif o in ('-d'):
        self.download_only = True
      elif o in ('-D'):
        print 'D: Enable debug.'
        Main.debug = True
      elif o in ('-n', '--dry-run'):
        self.dry_run = True
      elif o in ('-H', '--hosts'):
        self.hosts = a