Post

RedHat VM last patched duration in days

RedHat VM last patched duration in days

On RedHat systems, rpm tracks the install timestamp of every package, which lets you calculate when the most recent patch was applied. I use this in compliance checks to flag VMs that haven’t been patched in too long. The date arithmetic in the last block converts seconds since epoch to days.

  • current date in numeric
1
$ date +%s
  • get all installed patches with date and name in human redable form
1
$ rpm -qa --queryformat '%{installtime} %{installtime:date} %{name}\n'
  • get all installed patches with date and name in human redable form, sort it using installtime and return top record
1
$ rpm -qa --queryformat '%{installtime} %{installtime:date} %{name}\n' | sort | tail -n 1
  • get all installed patches installtime only, sort it using installtime and return top record
1
$ rpm -qa --queryformat '%{installtime}\n' | sort | tail -n 1
  • putting it all together and substracting dates
1
2
3
4
$ current_date=$(date +%s)
$ last_path_installed_date=$(rpm -qa --queryformat '%{installtime}\n' | sort | tail -n 1)
$ let last_patched=($current_date - $last_path_installed_date)/86400
$ echo $last_patched
This post is licensed under CC BY 4.0 by the author.