An easier to parse version of “yum history”

I got interested today in trying to come up with a solid way of determining when updates were last applied to a RHEL-derived Linux instance. Previously we’d been inferring it from the kernel version, but it turns out there is a convenient “yum history”  or “dnf history” command which will show you all the previous transactions that the package database has seen. However, the output is hard to parse in a script.

So here instead, is a little python script which does the thing. Feel free to mangle it to meet your needs:

import time

import dnf.base


b = dnf.base.Base()
for transaction in b.history.old():
    print('---------------------------------------------------')
    print(f'Transaction id: {transaction.tid}')
    print(f'Command line: {transaction.cmdline}')
    print(f'UID: {transaction.loginuid}')
    print(f'Return code: {transaction.return_code}')

    start = time.strftime('%Y-%m-%d %H:%M', time.localtime(transaction.beg_timestamp))
    end = time.strftime('%Y-%m-%d %H:%M', time.localtime(transaction.end_timestamp))
    elapsed = transaction.end_timestamp - transaction.beg_timestamp
    print(f'Duration: {start} -> {end} ({elapsed} seconds)')

    for tran in transaction.packages():
        if tran.is_package():
            details = f'{tran.name} {tran.version}'
        elif tran.is_group():
            g = tran.get_group()
            packages = []
            for pkg in g.getPackages():
                packages.append(pkg.getName())
            details = f'Group "{g.getName()}" ({", ".join(packages)})'
        elif tran.is_environment():
            e = tran.get_environment()
            details = f'Environment "{e.getName()}"'
        else:
            details = '...unknown transaction type!'

        print(f'    {tran.action_name} {details}')

    print()

To find only whole system updates, you’d look for command lines containing “upgrade” I suspect.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.