Esempio n. 1
0
def main():
  """ Tries to stop all Monit services until they are stopped. """
  monit_operator = MonitOperator()
  hostname = socket.gethostname()

  print('Waiting for monit to stop services')
  stopped_count = 0
  while True:
    entries = monit_operator.get_entries_sync()
    services = {service: state for service, state in entries.items()
                if 'cron' not in service and service != hostname}
    running = {service: state for service, state in services.items()
               if state not in (MonitStates.STOPPED, MonitStates.UNMONITORED)}
    if not running:
      print('Finished stopping services')
      break

    if len(services) - len(running) != stopped_count:
      stopped_count = len(services) - len(running)
      print('Stopped {}/{} services'.format(stopped_count, len(services)))

    try:
      service = next((service for service in sorted(running.keys())
                      if services[service] != MonitStates.PENDING))
      subprocess.Popen(['monit', 'stop', service])
    except StopIteration:
      # If all running services are pending, just wait until they are not.
      pass

    time.sleep(.3)
Esempio n. 2
0
def main():
    """ Tries to stop all Monit services until they are stopped. """
    logging.basicConfig(format=LOG_FORMAT, level=logging.INFO)
    monit_operator = MonitOperator()
    hostname = socket.gethostname()

    logging.info('Waiting for monit to stop services')
    logged_service_warning = False
    stopped_count = 0
    while True:
        entries = monit_operator.get_entries_sync()
        services = {
            service: state
            for service, state in entries.items()
            if 'cron' not in service and service != hostname
        }
        running = {
            service: state
            for service, state in services.items()
            if state not in (MonitStates.STOPPED, MonitStates.UNMONITORED)
        }
        if not running:
            logging.info('Finished stopping services')
            break

        if len(services) - len(running) != stopped_count:
            stopped_count = len(services) - len(running)
            logging.info('Stopped {}/{} services'.format(
                stopped_count, len(services)))

        try:
            ordered_services, unrecognized_services = order_services(
                running.keys())
            if unrecognized_services and not logged_service_warning:
                logging.warning('Unrecognized running services: {}'.format(
                    unrecognized_services))
                logged_service_warning = True

            ordered_services = ordered_services + unrecognized_services
            service = next((service for service in ordered_services
                            if services[service] != MonitStates.PENDING))

            monit_retry = retry(max_retries=5,
                                retry_on_exception=DEFAULT_RETRIES)
            send_w_retries = monit_retry(monit_operator.send_command_sync)
            send_w_retries(service, 'stop')
        except StopIteration:
            # If all running services are pending, just wait until they are not.
            pass

        time.sleep(.3)
Esempio n. 3
0
def main():
  """ Tries to stop all Monit services until they are stopped. """
  logging.basicConfig(format=LOG_FORMAT, level=logging.INFO)
  monit_operator = MonitOperator()
  hostname = socket.gethostname()

  logger.info('Waiting for monit to stop services')
  logged_service_warning = False
  stopped_count = 0
  while True:
    entries = monit_operator.get_entries_sync()
    services = {service: state for service, state in entries.items()
                if 'cron' not in service and service != hostname}
    running = {service: state for service, state in services.items()
               if state not in (MonitStates.STOPPED, MonitStates.UNMONITORED)}
    if not running:
      logger.info('Finished stopping services')
      break

    if len(services) - len(running) != stopped_count:
      stopped_count = len(services) - len(running)
      logger.info(
        'Stopped {}/{} services'.format(stopped_count, len(services)))

    try:
      ordered_services, unrecognized_services = order_services(running.keys())
      if unrecognized_services and not logged_service_warning:
        logger.warning(
          'Unrecognized running services: {}'.format(unrecognized_services))
        logged_service_warning = True

      ordered_services = ordered_services + unrecognized_services
      service = next((service for service in ordered_services
                      if services[service] != MonitStates.PENDING))

      monit_retry = retry(max_retries=5, retry_on_exception=DEFAULT_RETRIES)
      send_w_retries = monit_retry(monit_operator.send_command_sync)
      send_w_retries(service, 'stop')
    except StopIteration:
      # If all running services are pending, just wait until they are not.
      pass

    time.sleep(.3)
Esempio n. 4
0
def main():
    """ Tries to stop all Monit services until they are stopped. """
    monit_operator = MonitOperator()
    hostname = socket.gethostname()

    print('Waiting for monit to stop services')
    stopped_count = 0
    while True:
        entries = monit_operator.get_entries_sync()
        services = {
            service: state
            for service, state in entries.items()
            if 'cron' not in service and service != hostname
        }
        running = {
            service: state
            for service, state in services.items()
            if state not in (MonitStates.STOPPED, MonitStates.UNMONITORED)
        }
        if not running:
            print('Finished stopping services')
            break

        if len(services) - len(running) != stopped_count:
            stopped_count = len(services) - len(running)
            print('Stopped {}/{} services'.format(stopped_count,
                                                  len(services)))

        try:
            service = next((service for service in sorted(running.keys())
                            if services[service] != MonitStates.PENDING))
            subprocess.Popen(['monit', 'stop', service])
        except StopIteration:
            # If all running services are pending, just wait until they are not.
            pass

        time.sleep(.3)