Created
April 13, 2017 23:18
-
-
Save sideangleside/20d40b93ca43f518b1c5d9b42aadfd29 to your computer and use it in GitHub Desktop.
delete Inactive systems from RHN.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage:
Reporting
Deleting