3

Is there a clean way to detect when apt-get upgrade or apt-get dist-upgrade was last run on an Ubuntu Server?

If there isn't a way to do this by default, where would be the best place to modify a script in order to track when apt-get upgrade or apt-get dist-upgrade has last been run.

5

The log file you are searching is: /var/log/apt/history.log

Here are 2 log entries:

Start-Date: 2014-04-15  14:15:35
Commandline: apt-get install python3-tk
Install: libxcb-dri2-0:amd64 (1.9.1-3ubuntu1, automatic), x11-utils:amd64 (7.7+1, automatic), tk8.5-lib:amd64 (8.5.11-2ubuntu4, automatic), tcl8.5-lib:amd64 (8.5.13-1ubuntu4, automatic), libllvm3.3:amd64 (3.3-5ubuntu4, automatic), libgl1-mesa-dri:amd64 (9.2.1-1ubuntu3, automatic), libglapi-mesa:amd64 (9.2.1-1ubuntu3, automatic), libtxc-dxtn-s2tc0:amd64 (0~git20121227-1, automatic), libxv1:amd64 (1.0.9-1, automatic), libutempter0:amd64 (1.1.5-4build1, automatic), libxss1:amd64 (1.2.2-1, automatic), libxcb-glx0:amd64 (1.9.1-3ubuntu1, automatic), libgl1-mesa-glx:amd64 (9.2.1-1ubuntu3, automatic), libdrm-nouveau2:amd64 (2.4.46-1ubuntu1, automatic), libxcb-shape0:amd64 (1.9.1-3ubuntu1, automatic), blt:amd64 (2.4z-7, automatic), tk8.5:amd64 (8.5.11-2ubuntu4, automatic), tcl8.5:amd64 (8.5.13-1ubuntu4, automatic), xterm:amd64 (278-1ubuntu3, automatic), libxxf86vm1:amd64 (1.1.3-1, automatic), libelf1:amd64 (0.157-1ubuntu1, automatic), libxxf86dga1:amd64 (1.1.4-1, automatic), python3-tk:amd64 (3.3.1-0ubuntu2)
End-Date: 2014-04-15  14:15:44


Start-Date: 2014-04-23  11:10:37
Commandline: apt-get dist-upgrade
Upgrade: mysql-server-core-5.5:amd64 (5.5.35-0ubuntu0.13.10.2, 5.5.37-0ubuntu0.13.10.1), mysql-server-5.5:amd64 (5.5.35-0ubuntu0.13.10.2, 5.5.37-0ubuntu0.13.10.1), mysql-client-core-5.5:amd64 (5.5.35-0ubuntu0.13.10.2, 5.5.37-0ubuntu0.13.10.1), mysql-client-5.5:amd64 (5.5.35-0ubuntu0.13.10.2, 5.5.37-0ubuntu0.13.10.1), mysql-common:amd64 (5.5.35-0ubuntu0.13.10.2, 5.5.37-0ubuntu0.13.10.1), libmysqlclient18:amd64 (5.5.35-0ubuntu0.13.10.2, 5.5.37-0ubuntu0.13.10.1), python3-distupgrade:amd64 (0.205.5, 0.205.6), mysql-server:amd64 (5.5.35-0ubuntu0.13.10.2, 5.5.37-0ubuntu0.13.10.1), ubuntu-release-upgrader-core:amd64 (0.205.5, 0.205.6)
End-Date: 2014-04-23  11:11:10
1

It became useful for us to have a slightly more easy and accurate answer to the question "when was the last time we patched this thing?". So I put this together. I tested it on 12.04 and 14.04 and 16.04. It returns reasonably accurate answers for that question.

Note: "reasonably accurate" probably isn't "completely accurate".

Note: "for that question" only.

(constructive) Comments Appreciated!

#!/usr/bin/perl

#------------------ subroutines --------------------

sub parseRecord {
    my $sdate = "";
    my $useful = 0;
    my $packages = 0;
    my @ptmp;
    while (my $recordLine = shift() ) {

       if ($recordLine =~ m/^Start-Date: ([\d\-]*).*/) {
          $sdate = $1;
       }
       elsif ($recordLine =~ m/^Commandline:.*upgrade/) {
          $useful = 1;
       }
       elsif ($recordLine =~ m/^Install: (.*)/) {
          $recordLine =~ s/\([^\)]*\)//g;
          @ptmp = split(/,/,$recordLine);
          $packages = $packages + $#ptmp + 1;
       }
       elsif ($recordLine =~ m/^Upgrade: (.*)/) {
          $recordLine =~ s/\([^\)]*\)//g;
          @ptmp = split(/,/,$recordLine);
          $packages = $packages + $#ptmp + 1;
       }
    }



    if ($useful) {
       return ($sdate,$packages);
    }
    else {
       return ("0",0);
    }
}


#------------------ main program --------------------

@lines = split(/\n/,`/bin/zcat -f /var/log/apt/history.log  /var/log/apt/history*gz`);
my %patchHash;
my $line;
my @inputLines;
my $pushDate = "";
my $pushNum = "";

foreach $line (@lines) {
    # all records separated by blank lines
    if ($line !~ /./) {
       # no-op
    }
    elsif ($line =~ m/^Start-Date: ([\d\-]*).*/) {
       @inputLines = ();
       push (@inputLines, $line);
    }
    elsif ($line =~ m/^End-Date: ([\d\-]*).*/) {
       ($pushDate, $pushNum) = parseRecord(@inputLines);
       if ($pushNum != 0) {
          $patchHash{$pushDate} += $pushNum;
       }
    }
    else {
       push (@inputLines, $line);
    }
}

foreach $pushDate (sort(keys(%patchHash))) {
   print "$pushDate $patchHash{$pushDate}\n";
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.