Skip to content

Instantly share code, notes, and snippets.

@mrcrilly
Created January 19, 2014 16:00
Show Gist options
  • Save mrcrilly/8506819 to your computer and use it in GitHub Desktop.
Save mrcrilly/8506819 to your computer and use it in GitHub Desktop.
Script for using MCollective's mco client and Foreman's API to check server visibility between the two systems. I obviously want all hosts in Foreman/Puppet to be visible via MCollective too. This simple Python script does a check for me, printing a list of servers that aren't visible between both systems (missing from one or the other). The onl…
import requests, shlex, json
from subprocess import Popen, PIPE
class MCollective:
def list_hosts(self):
process = Popen(shlex.split("mco rpc rpcutil ping -j"), stdout=PIPE)
j = process.communicate()[0]
process.wait()
if j:
return [ i['sender'].split('.')[0] for i in json.loads(j) ]
else:
return None
class Foreman:
def __init__(self, hostname=None, username=None, password=None):
self.username = username
self.password = password
self.hostname = hostname
self.perpage = 100
self.sslverify = False
def list_hosts(self):
uri = "{0}/api/hosts?per_page={1}".format(self.hostname, self.perpage)
http = requests.get(uri, auth=(self.username, self.password), verify=self.sslverify)
if http.status_code == 200:
return [i['host']['name'].split('.')[0] for i in http.json()]
else:
return None
if __name__ == '__main__':
# Use whatever passwords you need to.
foreman_hosts = Foreman('foreman', 'admin', 'chageme').list_hosts()
mcollective_hosts = MCollective().list_hosts()
diff = [i for i in foreman_hosts if i not in mcollective_hosts]
if len(diff) > 0:
print diff
else:
print "All in sync."
@mrcrilly
Copy link
Author

For those wondering, it's written in Python and not Ruby (as Ruby has a native MCollective Ruby gem) because Python's requests library is some-what unmatched and works very well for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment