Skip to content

Instantly share code, notes, and snippets.

@owk
Created June 6, 2018 10:42
Show Gist options
  • Save owk/bab9d45cc60eba6295fe4a3cb8176c6f to your computer and use it in GitHub Desktop.
Save owk/bab9d45cc60eba6295fe4a3cb8176c6f to your computer and use it in GitHub Desktop.
def get_service_ids(names):
services_list, status = get_url(daemon + '/services')
if status == 406:
critical("Error checking service status, node is not in swarm mode")
return []
elif status not in HTTP_GOOD_CODES:
unknown("Could not retrieve service info")
return []
all_services_ids = dict((x['Spec']['Name'],x['ID']) for x in services_list)
if 'all' in names:
return all_services_ids
filtered = dict()
for matcher in names:
found = False
for candidate, value in all_services_ids.items():
if re.match("^{}$".format(matcher), candidate):
filtered[candidate] = value
found = True
# If we don't find a service that matches out regex
if not found:
critical("No services match {}".format(matcher))
return filtered
def get_tasks_for_services(names):
tasks_list, status = get_url(daemon + '/tasks')
if status == 503:
critical("Error checking task status, node is not part of a swarm")
return []
elif status not in HTTP_GOOD_CODES:
unknown("Could not retrieve service info")
return []
filtered_service_ids = get_service_ids(names)
selected_tasks = dict()
for name, id in filtered_service_ids.items():
for task in tasks_list:
if task["ServiceID"] == id:
current_earliest_task = task
if name in selected_tasks:
current_earliest_task = earliest_swarm_task(task,selected_tasks[name])
selected_tasks[name] = current_earliest_task
return selected_tasks
def process_task_status(task, ok_msg=None, critical_msg=None, warning_msg=None, unknown_msg=None):
if task["Status"]["State"] not in ["ready","rejected","shutdown","failed","remove","orphaned","preparing","assigned"]:
if task["Status"]["State"] == "running":
return ok(ok_msg + ': Running since {}'.format(humanize_timespan(task["Status"]["Timestamp"])))
return ok(ok_msg + ': '+ str(task["Status"]))
elif task["Status"]["State"] in ["ready","rejected","shutdown","failed","remove","orphaned"]:
return critical(critical_msg + ': '+task["Status"]["Err"])
elif task["Status"]["State"] in ["preparing","assigned"]:
return warning(warning_msg + ': '+task["Status"]["Message"])
else:
return unknown(unknown_msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment