Generate report of results from a Scheduled Job - RHN Satellite

Latest response

I frequently run remote commands against my environment to gather simple information. For example, if I wanted to know which hosts had kdump running at the time the script ran
service kdump status && exit 0 || exit 1

Which then creates a list of those that are and are-not running kdump. Unfortunately when you browse via the Sat Portal, the hosts are grouped by 25 hosts per page.

Does anyone know of an API call that I could submit which would return a list of hosts, either based on exit 0 (successful vs failed) or with the exit code in the output?

Responses

Sorry for the delay here James. We're actually still trying to chase up an API solution for this!

Hello James,

I think it can be done by the RHN API. IN

Follow bellow a script which we used in the past to accomplish a similar task. Basically the remote_command.py script will open the script.sh which contains the script itself, and the Python code will execute this on all the system registered. After sleeping 30 seconds, the Python script connects on all the registered servers again and print the output. For huge environments, increase the sleep time or slip the scripts.

#!/usr/bin/python

import xmlrpclib
import sys
import datetime
import time

SATELLITE_URL = "http://address-here/rpc/api"
SATELLITE_LOGIN = "sat-login"
SATELLITE_PASSWORD = "sat-password"
server = xmlrpclib.Server(SATELLITE_URL, verbose=0)
token = server.auth.login(SATELLITE_LOGIN, SATELLITE_PASSWORD)
job_list = []

print "\n\t##### Run Remote Command ######\n"

script_file = raw_input("Enter the script filename to be executed at remote servers: ")
# open the script
try:
f = open(script_file,"r")
script = f.read()
f.close()
except:
print "Unable to read the script file %s" %script_file
sys.exit(1)

# set date to execute script (yes.. past time)
date_run = datetime.datetime(2011,9,21,04,00,00)

systems_list = server.system.listSystems(token)
print "\nScheduling jobs"
print "-" * 20
for sys_sched in systems_list:
job = server.system.scheduleScriptRun(token,sys_sched['id'],
"root", "root", 600, script, date_run)
print "\tJob %s scheduled at %s" %(job,sys_sched['name'])
job_list.append(dict(job_id=job,system=sys_sched['id']))

time.sleep(30)

for system in systems_list:
sys_cpu = server.system.getCpu(token,int(system['id']))
print "=" * 10
print "Hostname: \t%s" %system['name']
print "ID: \t%s" %system['id']
print "CPU Model: \t%s" %sys_cpu['model']
print "Vendor: \t%s" %sys_cpu['vendor']
print "Cores: \t%s" %sys_cpu['count']
# only the systems with osad enabled
if len(job_list):
for job in job_list:
if job['system'] == system['id']:
job_details = server.system.getScriptResults(token,job['job_id'])
for r in job_details:
#print "Sockets: \t%s" %r['output']
print "Remote Output:"
print "-" * 10
print r['output']

server.auth.logout(token)

Cheers
Fábio Da Cunha

Thanks for this Fabio (Sorry I didn't see it previously!)

For this type of tasks I would use ansible (puppet/chef-like software but much, much easier to use - it took me about half an hour to start using it).

Thanks Fabio - that will definitely get me started (I'm not much of a python person yet... ;-)

Thanks Przemyslaw - unfortunately we don't have much flexibility in alternate solutions at this point. We run pretty lean, so any additional things to manage make my team nervous. That is a good suggestion though.

James,

I assure you that getting to know Python is much, much more difficult then to start using Ansible.
I find (and like !) Python very easy to get to know (at least basics) but Ansible (it is not a language !) is way much easier - just try that :-).
Example line which copies your local script to remote server:
...
copy: src=/tmp/example-script.sh dest=/tmp/ owner=root group=root mode=644
...
Next you would run this script but I leave it as a homework ;-)

It is even easier to run a script remotely. The complete playbook would look like:

- hosts: all
  tasks:
  - script: /path/to/script.sh --arg

The online documentation of Ansible is quite good and details the usage of the script module.

Hi James,

Commenting here from the invite you mentioned at https://access.redhat.com/site/discussions/723333 ...

By the way, you can install/run spacecmd on a system that is not your Satellite server.
I tested it on a different systems (that are not my satellite servers) and it worked on both (with "-s")!

The only difference, is when you enter the spacecmd command UI from a system that is not your Satellite server, do this:

[notrootuser@workstation2 ~]# spacecmd -s yoursatserver@fqdn.com (change "yoursatserver@fqdn.com" to your fully qualified hostname & domain).

The benefit is that you can then run scripts from your local system (protect them from others and make completely sure there is no chance they can be edited by anyone else but you (be careful, take extra measures).
Then in the spacecmd command UI, you can site the path/file with the '-f' bit, and not have to publish the scripts to the satellite server first.

I too would like a method where you could pass the script using spacecmd. I think it would really be through a perl API script (I'm working on those now, nearly there with a working template for some of the things I wish to do).

duplicate post - whoops

James, until I can finish my perl API script (nearly there), I'm using a quick work-around for some of this...

I periodically/persistently get request of what rpm and version is loaded on a specific system (among other requests).

Anyway, this is from the other thread for reference here. When I get the perl script template finished, I'll post it here (I get all rpms, nearly done with "next if" in the perl directives to narrow ths scope).

Sometimes our management or security folks are interested in a list of servers with the specific version with the minor point release. A command to produce this helps to generate the report they request periodically.
DISCLAIMER: Yes, the mere output of /etc/redhat-release should never be used to infer security compliance, that is a stark given for many obvious reasons!
However - this is useful to determine what sort of rpm (not just redhat-release) is installed and what version.

Thanks to those for mentioning spacecmd from EPEL.
Here's a small rough query using spacecmd from EPEL. Shows the systems and their versions:


#!/bin/bash fqdn=acme.com /usr/bin/spacecmd package_listinstalledsystems redhat-release | egrep -v '^\#|^\-' | uniq

I experimented and pushed this script below as a scheduled query to my system group called "everything"
NOTE: some people change the /etc/redhat-release just to load some third party software, so querying the rpm is probably better.

[notroot@workstation1 ~]# cat /path/to/my/scripts/queryrhelver.sh
#!/bin/bash
echo -ne `hostname -s` ": " `rpm -q redhat-release` " \n"

- Or perhaps you wish to check for the current glibc version

#!/bin/bash
echo -ne `hostname -s` ": " `rpm -q glibc` " \n"
echo

Then run the script to my system group named "everything"

First enter the spacecmd command UI
[notroot@workstation1 ~]# spacecmd -s SATSERVERNAME@FQDN
spacecmd {SSM:0}> system_runscript -f /path/to/my/scripts/queryrhelver.sh group:everything
User:       root
Group:      root
Timeout:    600 seconds
Start Time: 20140216T22:21:35

Script Contents
---------------
#!/bin/bash
echo -n `hostname -s` ": " `rpm -q redhat-release`
echo


Systems
-------
four0four.myfqdn.com
three0one.myfqdn.com
---truncated output---

Is this ok [y/N]: y
INFO: Action ID: 1134
INFO: Scheduled: XXXX system(s)

Get the ID that the script ran from the use of spacecmd above (it is above too):

[notroot@workstation1 ~]# spacecmd schedule_listcompleted | grep arbitrary
INFO: Connected to https://myrhnserver/rpc/api as youknowwho
1134     20140216T16:51:18     XXXX    0    0    Run an arbitrary script
--truncated list---

Wait for the cron to pick up the 'rhn_check' command, then view completed scheduled script:

[notroot@workstation1 ~]# spacecmd schedule_listcompleted
INFO: Connected to https://myrhnserver/rpc/api as youknowwho
ID      Date                 C    F    P     Action
--      ----                ---  ---  ---    ------
1134     20140216T16:51:18     XXXX    0    0    Run an arbitrary script

Then view the output:
Important Note: Sadly, one can only run the egrep below from the actual Satellite server.


[notroot@myrhnserver ~]# fqdn=acme.com [notroot@myrhnserver ~]# spacecmd schedule_getoutput 1134 | egrep '$fqdn|release' INFO: Connected to https://myrhnserver/rpc/api as youknowwho four0four : Red Hat Enterprise Linux Server release 6.5 (Santiago) three0one : Red Hat Enterprise Linux Server release 6.5 (Santiago) ---truncated---

I wish there was a method to redirect the output of the spacecmd (to a file) while on a different system (when not logged into the Satellite server).

interesting discussion - I have a question as well regarding the API.

I need to generate a list of all hosts that are part of a System group (listGroups) that have updates available via the API(listOutOfDateSystems)

Example:

Groupname (System groups)

Patched systems : yy
Unpatched systems : xx

I know parts of this is possible via the API by using some of the API methods but I can't find a way to get each of the outofdate systems listed under each of the system groups.

Any ideas ? Thanks

SMG UNIX,

Based on the returns, I'd say no, you'd need to make the association of system to group.

The systemsgroup calls don't seem to return the right info, so best I can see is use listOutOfDate, collect the system ids, and build an array of groups with listGroup.

I love Ansible, and I often automate stuff using the RHN API, but for something as simple as this, nothing beats shmux (shell multiplexor). This tool written in C uses openssh for running remote ad-hoc tasks on your infrastructure, and it does this blazingly fast.

I often run 250 parallel connections on our 3000+ servers to get simple reports or execute operations. In this case all that is needed is:

shmux -m -M250 -Sall -c 'service kdump status' host1 host2 host3 ...

Or if you have a file listing all your hosts, you can do

shmux -m -M250 -Sall -c 'service kdump status' - < hosts.txt

shmux will automatically list the ones with a non-zero return code, but the command line options allow you to modify what means success and what means failure.

For 3000 systems it takes less than 30 seconds to complete. Nothing can beat that, not even Ansible.

More information regarding shmux is available from:

Thanks for the info Dag!

James, see the bit from Dag Wieers above too.

James, spacecmd now comes standard/preinstalled with satellite 5.7, without having to bother to grab it from EPEL.

Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.