Category Archives: Scripts

Quick Ubuntu Package Update Script

I don’t want to have to remember to login to my syslog server and check for required updates. Here is the script I wrote and added to cron so that I don’t have to:

#!/usr/bin/perl

use Email::Sender::Simple qw(sendmail);
use Email::Simple;
use Email::Sender::Transport::Sendmail qw();
use Try::Tiny;

my $to = "noc\@howfantastic.net";
my $from = "root\@syslog.howfantastic.net";
my $subject = "update script";
my $body =  `/usr/bin/apt-get update && /usr/bin/apt-get upgrade -s`;

my $email = Email::Simple->create(
    header=>[To=>$to, From=>$from,
             Subject=>$subject],
    body=>$body,
);

try {
    sendmail($email,
             {from=>$from,
             transport=>Email::Sender::Transport::Sendmail->new});
} catch {
    print "Can't send mail: $_";
}

To get this to work I needed to install some packages:

apt-get install sendmail
apt-get install libemail-sender-perl 
apt-get install libemail-simple-perl

 

I then added my script to cron:

root@itssys01:~# crontab -l
# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
0 0 * * * /usr/local/bin/update-check.pl > /dev/null

Now I get an email with a list of packages which would be updated were I to run the following:

/usr/bin/apt-get upgrade

I don’t want my system blindly updating itself so this way I can still check things over before committing.

Cisco Syslog Summary Script

Notes on the script

A while back I came across a nice script called summarize-nmslog by Darin Davis. I wanted to adapt it for use in our environment and ended up rewriting it. There is more work to do – I’d like to update it to use Config::TIny rather than global variables. A couple of notes on those:

  1. I’ve specified the DNS server as 8.8.8.8. If you run local DNS for RFC1918 addressed kit you can change this.
  2. You’ll need to enter the path and file format of your logs.

Here is the section of code I mean.

 26 my $DNS = "\@8.8.8.8";
 27 my $DIGLOOKUP = "/usr/bin/dig $DNS +short -x ";
 28 my $LOGDIR = '/var/log/remote';
 29 my $yesterdaylogfile = "$LOGDIR/backbone/backbone-$yesterday";
 30 my $todaylogfile = "$LOGDIR/backbone-$today";
 31 my $dt = DateTime->now();
 32 my $today = $dt->ymd('-');
 33 $dt->subtract( days => 1 );
 34 my $yesterday = $dt->ymd('-');

As you can see our logs are in /var/log/cisco and a nightly cronjob rotates them into /var/log/cisco-YYYY-MM-DD. The script assumes this – you’ll need to customise it to your own environment.

Some of the output deals with mac flaps as we had a problem with those related to being forced to trunk VLANs through our core, always a Very Bad Idea. Hopefully you won’t need that and can delete it. Finally, it assumes you only use VLANs 1-999, although that is easily fixed.

Here is a simplified output of the script – the real one had rather more entries.

guym@logserver:~$ /usr/local/bin/cisco_syslog_report 
Summary of Cisco syslog Messages in /var/log/cisco-2012-08-08 

Any devices with no DNS entries listed below:

 Cisco message types seen in the logs:

   9	PIM-5-NBRCHG
   2 	OSPF-5-ADJCHG
   2 	SYS-6-LOGGINGHOST_STARTSTOP
   1 	SSH-4-SSH2_UNEXPECTED_MSG
   1    STORM_CONTROL-3-SHUTDOWN

Messages sorted by frequency and source device:

     10 	   router71.domain     192.168.22.71 	 PIM-5-NBRCHG
     9 	           router11.domain     192.168.22.11 	 PIM-5-NBRCHG
     8 	           router81.domain     192.168.22.81 	 OSPF-5-ADJCHG
     8 	           router81.domain     192.168.22.81 	 PIM-5-NBRCHG
     8 	           router61.domain     192.168.22.61 	 PIM-5-NBRCHG
     8 	           router31.domain     192.168.22.31 	 PIM-5-NBRCHG
     8 	           router21.domain     192.168.22.21 	 PIM-5-NBRCHG
     8 	           router51.domain     192.168.22.51 	 PIM-5-NBRCHG
     1 	           router200.domain    192.168.22.200 	 SYS-6-LOGGINGHOST_STARTSTOP
     1 	           router3.domain      192.168.21.4 	 SYS-6-LOGGINGHOST_STARTSTOP
     1 	           router1.domain      192.168.22.1 	 PIM-5-NBRCHG
     1 	           router2.domain      192.168.22.2 	 OSPF-5-ADJCHG
     1 	           router2.domain      192.168.22.2 	 PIM-5-NBRCHG
     1 	           router2.domain      192.168.22.2 	 SSH-4-SSH2_UNEXPECTED_MSG
     1 	           switch1.domain      172.16.7.9 	 STORM_CONTROL-3-SHUTDOWN

Top Ten MAC flappers of the day:

	1:	Vlan 713 flapped 10 times
	2:	Vlan 997 flapped 8 times
	3:	Vlan 698 flapped 4 times
	4:	Vlan 214 flapped 3 times
	5:	Vlan 691 flapped 2 times
	6:	Vlan 695 flapped 1 times
	7:	Vlan 151 flapped 1 times
	8:	Vlan 902 flapped 1 times
	9:	Vlan 994 flapped 1 times
	10:	Vlan 995 flapped 1 times

Summary

To make use of this script, I run it once an hour via a cronjob and pipe the output to a file which is read by some other perl and published to an internal website. It has proved to be a very useful way of baselining our network and sped up troubleshooting once or twice. I’ve attached it to this post here, feel free to make use of it or give me some feedback.