root/trunk/misc/shutdown_notification.pl

Revision 126, 2.4 KB (checked in by mbooth, 8 months ago)

Add some handy bits of script that I've written over the years:

  • Shell script that creates and configures samba shares on the raid array.
  • Perl script that emails the admins of hosted websites when the box is going down for a sheduled reboot.
  • Python script that attempts to spot inconsistencies and errors in the DNS config for a given domain and subnet.
  • Property svn:executable set to *
Line 
1#!/usr/bin/perl -w
2
3use strict;
4
5use Getopt::Long;
6use Net::SMTP::TLS;
7
8# parse command line options
9my $reboot_flag = '1';
10my $shutdown_time = '04:00';
11GetOptions('reboot!' => \$reboot_flag, 'time=s' => \$shutdown_time);
12unless($shutdown_time =~ m/^[012]\d:[012345]\d$/) {
13    print "Invalid time: $shutdown_time\n";
14    exit 1;
15}
16
17# email server config
18my $smtp_server = 'smtp.gmail.com';
19my $smtp_user = 'mat.booth@gmail.com';
20my $smtp_password = 'music_trend';
21my $smtp_port = 587;
22
23# get list of each vhosts' admin email addresses
24my %sites;
25foreach my $conf (glob "./*/site.conf") {
26    open CONF, $conf or die($!);
27    $conf =~ s/\.\/(.*)\/site\.conf/$1/;
28    while(<CONF>) {
29        chomp;
30        $sites{$conf} = $_ if s/^ServerAdmin\s+(\S*)/$1/i;
31    }
32}
33
34foreach my $vhost (keys %sites) {
35
36    # email config
37    my $email_to = $sites{$vhost};
38    my $email_from = 'admin@matbooth.co.uk';
39    my $email_subject = "$vhost is going down for maintenance!";
40    my $email_replyto = 'admin@matbooth.co.uk';
41
42    # build smtp headers
43    my @head;
44    push(@head, "To: $email_to\n");
45    push(@head, "From: $email_from\n");
46    push(@head, "Subject: $email_subject\n");
47    push(@head, "Reply-to: $email_replyto\n");
48    push(@head, "Content-Type: text/plain; charset=UTF-8\n");
49    push(@head, "Content-Transfer-Encoding: 8bit\n");
50    push(@head, "\n");
51
52    # build message body
53    my @body;
54    push(@body, "Dear Webmaster,\n\n");
55    push(@body, "Recent security related kernel updates require that we reboot the server your website is hosted on. All services at $vhost will be unavailable from $shutdown_time for approximately 5-10 minutes. If this is inconvenient, please email me and I will reschedule the reboot for another time.\n\n");
56    push(@body, "Peace out,\nMat\n\n");
57    push(@body, "------------------------------\n\n");
58    push(@body, "This is an automatically generated message. You are receiving it because the ServerAdmin directive in the Apache virtual host config for $vhost is set to this email address. If you do not believe this message was intended for you, please email me immediately so that I can correct the mistake.\n");
59    push(@body, "\n");
60
61    # send email
62    my $smtp = new Net::SMTP::TLS($smtp_server, Port => $smtp_port, User => $smtp_user, Password => $smtp_password);
63    $smtp->mail($email_from);
64    $smtp->to($email_to);
65    $smtp->data();
66    $smtp->datasend(@head, @body);
67    $smtp->dataend();
68    $smtp->quit();
69}
70
71# issue shutdown command
72if($reboot_flag eq "1") {
73    system "shutdown -r $shutdown_time &";
74} else {
75    system "shutdown -h $shutdown_time &";
76}
77exit 0;
Note: See TracBrowser for help on using the browser.