Skip to content

Instantly share code, notes, and snippets.

@sideangleside
Created April 13, 2017 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sideangleside/20d40b93ca43f518b1c5d9b42aadfd29 to your computer and use it in GitHub Desktop.
Save sideangleside/20d40b93ca43f518b1c5d9b42aadfd29 to your computer and use it in GitHub Desktop.
delete Inactive systems from RHN.
#!/usr/bin/env python
"""
File: deleteInactiveProfiles.py
Author: Rich Jerrido <rwj@redhat.com>
Purpose: Given a username/password & URL; report on (and optionally delete) all inactive
profiles on RHN
"""
import getpass
import sys
import xmlrpclib
import ssl
import time
from datetime import date
from optparse import OptionParser
today = date.today()
parser = OptionParser()
parser.add_option("-l", "--login", dest="login", help="Login user for satellite", metavar="LOGIN")
parser.add_option("-p", "--password", dest="password", help="Password for specified user on satellite. Will prompt if omitted", metavar="PASSWORD")
parser.add_option("-s", "--server", dest="serverfqdn", help="FQDN of satellite server - omit https://", metavar="SERVERFQDN")
parser.add_option("-d", "--days", dest="daysback", type=int, help="Number of days of inactivity", metavar="DAYSBACK")
parser.add_option("-r", "--remove", action="store_true", dest="remove", help="Whether to Remove servers (true if present)", default=False)
(options, args) = parser.parse_args()
if not ( options.login and options.serverfqdn and options.daysback ):
print "Must specify login, server, and daysback options. See usage:"
parser.print_help()
print "\nExample usage: ./deleteInactiveProfiles.py -l admin -p password -s satellite.example.com -d 5"
sys.exit(1)
else:
login = options.login
password = options.password
serverfqdn = options.serverfqdn
daysback = options.daysback
remove = options.remove
if not password: password = getpass.getpass("%s's password:" % login)
SATELLITE_URL = "https://%s/rpc/api" % serverfqdn
SATELLITE_LOGIN = login
SATELLITE_PASSWORD = password
if hasattr(ssl, '_create_unverified_context'):
ssl._create_default_https_context = ssl._create_unverified_context
client = xmlrpclib.Server(SATELLITE_URL, verbose=0)
key = client.auth.login(SATELLITE_LOGIN, SATELLITE_PASSWORD)
servers = client.system.listUserSystems(key)
print '*** Servers meeting the criteria of having not checked in within the past %s days' % daysback
print 'Server ID , Server Name , Last Check-in'
for server in servers:
rhndate=str(server['last_checkin'])
working_rhn_date=date(int(rhndate[0:4]), int(rhndate[5:7]), int(rhndate[8:10]))
days_ago = abs(today - working_rhn_date)
if days_ago.days > daysback:
print str(server['id']) + ' , ' + server['name'] + ' , ' + str(server['last_checkin'])
if remove:
print '[REMOVING] %s' % server['name']
client.system.deleteSystems(key,int(server['id']))
client.auth.logout(key)
sys.exit(0)
@sideangleside
Copy link
Author

Example usage:

Reporting

./deleteInactiveProfiles.py -l RHN_USER -p 'RHN_PASS' -s xmlrpc.rhn.redhat.com --days 15
*** Servers meeting the criteria of having not checked in within the past 15 days
Server ID , Server Name , Last Check-in
1040950702 , devnode-104.example.com , 2017-01-15 10:38:31.0
1040950643 , proxy.example.org , 2017-01-15 19:16:44.0

Deleting

./deleteInactiveProfiles.py -l RHN_USER -p 'RHN_PASS' -s xmlrpc.rhn.redhat.com --days 15 --remove
*** Servers meeting the criteria of having not checked in within the past 15 days
Server ID , Server Name , Last Check-in
1040950702 , devnode-104.example.com , 2017-01-15 10:38:31.0
[REMOVING] devnode-104.example.com
1040950643 , proxy.example.org , 2017-01-15 19:16:44.0
[REMOVING] proxy.example.org

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