From b7df6b75de6e6ac98d9a1d95a2b238420369ce9c Mon Sep 17 00:00:00 2001 From: mischa Date: Sun, 9 May 2021 09:55:58 +0200 Subject: [PATCH] restructure of notify.pl --- notify.pl | 578 +++++++-------------------------------------------- vms/vm01.txt | 2 +- 2 files changed, 76 insertions(+), 504 deletions(-) diff --git a/notify.pl b/notify.pl index 05de4d5..849b03a 100755 --- a/notify.pl +++ b/notify.pl @@ -16,8 +16,7 @@ # # vmm(4)/vmd(8) VM notify script for OpenBSD Amsterdam # 2020/05/17 initial release -# -# This is WIP, currently there is a lot of overlap in the functions. +# 2021/05/09 complete restructure and KISS # use 5.024; use strict; @@ -26,7 +25,7 @@ use autodie; use POSIX qw(strftime); use HTTP::Tiny; -# Get function and function_variable (vmid) from arguments +# get function and function_variable (vmid) from arguments my $function = $ARGV[0] || "empty"; my $function_variable = $ARGV[1] || "empty"; @@ -65,9 +64,10 @@ sub get_variables { return %hash; } -sub deployed { +sub mailout { my %conf = %{$_[0]}; my %vms = %{$_[1]}; + my $_etc = $conf{'conf'}{'ETC'}; my $_vms = $conf{'conf'}{'VMS'}; my $_tmpl = $conf{'conf'}{'TEMPLATES'}; @@ -75,145 +75,11 @@ sub deployed { my $_ip4netmask = $conf{'conf'}{'NETMASK'}; my $_ip4gw = $conf{'conf'}{'ROUTER'}; - my $template = "$_tmpl/email-deployed.txt"; - - my $year = strftime("%Y", localtime); - my $month = strftime("%m", localtime); + my $template = "$_tmpl/email-$function.txt"; my $server_number = $1 if $_server =~ /([0-9]+)/; my $oddeven = $server_number % 2; - my @stopped_vms = qx(vmctl show | grep stopped | awk '{print \$9}'); - - for my $vm_name (sort keys %vms) { - my $_date = $vms{$vm_name}{'date'}; - my $_payment = $vms{$vm_name}{'payment'}; - my $_donated = $vms{$vm_name}{'donated'}; - my $_name = $vms{$vm_name}{'name'}; - my $_email = $vms{$vm_name}{'email'}; - my $_hostname = $vms{$vm_name}{'hostname'}; - - if ($function_variable =~ /empty/) { print "No VMID provided\n"; last; } - if ($function_variable =~ /$vm_name/) { - (my $_firstname, my $_lastname) = split(/ /, $_name, 2); - my $ip4address = qx(grep -A2 $vm_name $_etc/dhcpd.conf | awk '/fixed-address/{print \$2}' | tr -d ';\n'); - my $ip6address = qx(grep -A3 $vm_name $_etc/dhcpd.conf | awk '/fixed-address-ipv6/{print \$2}' | tr -d ';\n'); - (my $ip6gw = $ip6address) =~ s/::.*/::1/; - - open(my $fh, '<', $template); - open my $fh_email, "|-", "/usr/sbin/sendmail -t"; - printf $fh_email "To: %s\n", $_email; - while (my $row = <$fh>) { - chomp $row; - $row =~ s/NAME/$_firstname/g; - $row =~ s/IPV4$/$ip4address/g; - $row =~ s/IPV4NETMASK$/$_ip4netmask/g; - $row =~ s/IPV4GW$/$_ip4gw/g; - $row =~ s/IPV6$/$ip6address/g; - $row =~ s/IPV6GW$/$ip6gw/g; - $row =~ s/VMID/$vm_name/g; - $row =~ s/SERVER/$_server/g; - - $row =~ s/YEAR/$year/g; - $row =~ s/TOTAL_DONATED/$total_donated/g; - $row =~ s/TOTAL_VMS/$total_vms/g; - $row =~ s/TOTAL_HOSTS/$total_hosts/g; - $row =~ s/PAYMENT/$_payment/g; - - if ($row =~ /TIME\((.*)\)/) { - my @TIMES = split(/,/, $1); - $row =~ s/TIME\(.*\)/$TIMES[$oddeven]/g; - } - - print $fh_email "$row\n"; - } - close $fh_email; - print "DEPLOYED: $_date, $_payment, $_name, $_email, $_hostname, $_server ($vm_name), $ip4address\n"; - } - } -} - -# function to collect stopped vms -sub stopped { - my %conf = %{$_[0]}; - my %vms = %{$_[1]}; - my $_etc = $conf{'conf'}{'ETC'}; - my $_vms = $conf{'conf'}{'VMS'}; - my $_tmpl = $conf{'conf'}{'TEMPLATES'}; - my $_server = $conf{'conf'}{'SERVER'}; - my $_ip4netmask = $conf{'conf'}{'NETMASK'}; - my $_ip4gw = $conf{'conf'}{'ROUTER'}; - - my $template = "$_tmpl/email-stopped.txt"; - my $year = strftime("%Y", localtime); my $month = strftime("%m", localtime); - my $server_number = $1 if $_server =~ /([0-9]+)/; - my $oddeven = $server_number % 2; - my @stopped_vms = qx(vmctl show | grep stopped | awk '{print \$9}'); - - for my $vm_name (sort keys %vms) { - my $_date = $vms{$vm_name}{'date'}; - my $_payment = $vms{$vm_name}{'payment'}; - my $_donated = $vms{$vm_name}{'donated'}; - my $_name = $vms{$vm_name}{'name'}; - my $_email = $vms{$vm_name}{'email'}; - my $_hostname = $vms{$vm_name}{'hostname'}; - - if (grep(/$vm_name/, @stopped_vms)) { - (my $_firstname, my $_lastname) = split(/ /, $_name, 2); - my $ip4address = qx(grep -A2 $vm_name $_etc/dhcpd.conf | awk '/fixed-address/{print \$2}' | tr -d ';\n'); - my $ip6address = qx(grep -A3 $vm_name $_etc/dhcpd.conf | awk '/fixed-address-ipv6/{print \$2}' | tr -d ';\n'); - (my $ip6gw = $ip6address) =~ s/::.*/::1/; - - open(my $fh, '<', $template); - open my $fh_email, "|-", "/usr/sbin/sendmail -t"; - printf $fh_email "To: %s\n", $_email; - while (my $row = <$fh>) { - chomp $row; - $row =~ s/NAME/$_firstname/g; - $row =~ s/IPV4$/$ip4address/g; - $row =~ s/IPV4NETMASK$/$_ip4netmask/g; - $row =~ s/IPV4GW$/$_ip4gw/g; - $row =~ s/IPV6$/$ip6address/g; - $row =~ s/IPV6GW$/$ip6gw/g; - $row =~ s/VMID/$vm_name/g; - $row =~ s/SERVER/$_server/g; - - $row =~ s/YEAR/$year/g; - $row =~ s/TOTAL_DONATED/$total_donated/g; - $row =~ s/TOTAL_VMS/$total_vms/g; - $row =~ s/TOTAL_HOSTS/$total_hosts/g; - $row =~ s/PAYMENT/$_payment/g; - - if ($row =~ /TIME\((.*)\)/) { - my @TIMES = split(/,/, $1); - $row =~ s/TIME\(.*\)/$TIMES[$oddeven]/g; - } - - print $fh_email "$row\n"; - } - close $fh_email; - print "STOPPED: $_date, $_payment, $_name, $_email, $_hostname, $_server ($vm_name), $ip4address\n"; - } - } -} - -sub renewal { - my %conf = %{$_[0]}; - my %vms = %{$_[1]}; - my $_etc = $conf{'conf'}{'ETC'}; - my $_vms = $conf{'conf'}{'VMS'}; - my $_tmpl = $conf{'conf'}{'TEMPLATES'}; - my $_server = $conf{'conf'}{'SERVER'}; - my $_ip4netmask = $conf{'conf'}{'NETMASK'}; - my $_ip4gw = $conf{'conf'}{'ROUTER'}; - - my $template = "$_tmpl/email-renewal.txt"; - - my $year = strftime("%Y", localtime); - my $month = strftime("%m", localtime); - my $server_number = $1 if $_server =~ /([0-9]+)/; - my $oddeven = $server_number % 2; - my @stopped_vms = qx(vmctl show | grep stopped | awk '{print \$9}'); my $response = HTTP::Tiny->new->get('https://openbsd.amsterdam/index.html'); my $total_donated = $1 if $response->{'content'} =~ /([0-9,]+) donated to the OpenBSD/; @@ -229,369 +95,47 @@ sub renewal { my $_name = $vms{$vm_name}{'name'}; my $_email = $vms{$vm_name}{'email'}; my $_hostname = $vms{$vm_name}{'hostname'}; + my $_username = $vms{$vm_name}{'username'}; - if ($_subscription =~ /yes/) { next; } - if ($_donated =~ /done/) { next; } - if ($_donated =~ /expire/) { next; } - if ($_donated =~ /sponsor/) { next; } - if ($_donated =~ /renewal/) { next; } - if ($_date =~ /$year\//) { next; } - if ($_date =~ /\/$month\//) { - (my $_firstname, my $_lastname) = split(/ /, $_name, 2); - (my $_year, my $_month, my $_day) = split(/\//, $_date, 3); - my $ip4address = qx(grep -A2 $vm_name $_etc/dhcpd.conf | awk '/fixed-address/{print \$2}' | tr -d ';\n'); - my $ip6address = qx(grep -A3 $vm_name $_etc/dhcpd.conf | awk '/fixed-address-ipv6/{print \$2}' | tr -d ';\n'); - (my $ip6gw = $ip6address) =~ s/::.*/::1/; + (my $_firstname, my $_lastname) = split(/ /, $_name, 2); + (my $_year, my $_month, my $_day) = split(/\//, $_date, 3); + my $ip4address = qx(grep -A2 $vm_name $_etc/dhcpd.conf | awk '/fixed-address/{print \$2}' | tr -d ';\n'); + my $ip6address = qx(grep -A3 $vm_name $_etc/dhcpd.conf | awk '/fixed-address-ipv6/{print \$2}' | tr -d ';\n'); + (my $ip6gw = $ip6address) =~ s/::.*/::1/; - open(my $fh, '<', $template); - open my $fh_email, "|-", "/usr/sbin/sendmail -t"; - printf $fh_email "To: %s\n", $_email; - while (my $row = <$fh>) { - chomp $row; - $row =~ s/NAME/$_firstname/g; - $row =~ s/IPV4$/$ip4address/g; - $row =~ s/IPV4NETMASK$/$_ip4netmask/g; - $row =~ s/IPV4GW$/$_ip4gw/g; - $row =~ s/IPV6$/$ip6address/g; - $row =~ s/IPV6GW$/$ip6gw/g; - $row =~ s/VMID/$vm_name/g; - $row =~ s/SERVER/$_server/g; + open(my $fh, '<', $template); + open my $fh_email, "|-", "/usr/sbin/sendmail -t"; + printf $fh_email "To: %s\n", $_email; + while (my $row = <$fh>) { + chomp $row; + $row =~ s/NAME/$_firstname/g; + $row =~ s/IPV4$/$ip4address/g; + $row =~ s/IPV4NETMASK$/$_ip4netmask/g; + $row =~ s/IPV4GW$/$_ip4gw/g; + $row =~ s/IPV6$/$ip6address/g; + $row =~ s/IPV6GW$/$ip6gw/g; + $row =~ s/VMID/$vm_name/g; + $row =~ s/SERVER/$_server/g; + $row =~ s/HOSTNAME/$_hostname/g; + $row =~ s/USERNAME/$_username/g; - $row =~ s/YEAR/$year/g; - $row =~ s/TOTAL_DONATED/$total_donated/g; - $row =~ s/TOTAL_VMS/$total_vms/g; - $row =~ s/TOTAL_HOSTS/$total_hosts/g; - $row =~ s/PAYMENT/$_payment/g; + $row =~ s/YEAR/$year/g; + $row =~ s/TOTAL_DONATED/$total_donated/g; + $row =~ s/TOTAL_VMS/$total_vms/g; + $row =~ s/TOTAL_HOSTS/$total_hosts/g; + $row =~ s/PAYMENT/$_payment/g; - if ($row =~ /TIME\((.*)\)/) { - my @TIMES = split(/,/, $1); - $row =~ s/TIME\(.*\)/$TIMES[$oddeven]/g; - } - - print $fh_email "$row\n"; + if ($row =~ /TIME\((.*)\)/) { + my @TIMES = split(/,/, $1); + $row =~ s/TIME\(.*\)/$TIMES[$oddeven]/g; } - close $fh_email; - print "RENEWAL: $_date, $_payment, $_name, $_email, $_hostname, $_server ($vm_name), $ip4address\n"; + print $fh_email "$row\n"; } + close $fh_email; + print "$function: $_date, $_payment, $_name, $_email, $_hostname, $_server ($vm_name), $ip4address\n"; } } -sub subscription { - my %conf = %{$_[0]}; - my %vms = %{$_[1]}; - my $_etc = $conf{'conf'}{'ETC'}; - my $_vms = $conf{'conf'}{'VMS'}; - my $_tmpl = $conf{'conf'}{'TEMPLATES'}; - my $_server = $conf{'conf'}{'SERVER'}; - my $_ip4netmask = $conf{'conf'}{'NETMASK'}; - my $_ip4gw = $conf{'conf'}{'ROUTER'}; - - my $template = "$_tmpl/email-subscription.txt"; - - my $year = strftime("%Y", localtime); - my $month = strftime("%m", localtime); - my $server_number = $1 if $_server =~ /([0-9]+)/; - my $oddeven = $server_number % 2; - my @stopped_vms = qx(vmctl show | grep stopped | awk '{print \$9}'); - - my $response = HTTP::Tiny->new->get('https://openbsd.amsterdam/index.html'); - my $total_donated = $1 if $response->{'content'} =~ /([0-9,]+) donated to the OpenBSD/; - my $total_vms = $1 if $response->{'content'} =~ /([0-9]+) VMs deployed/; - $response = HTTP::Tiny->new->get('https://openbsd.amsterdam/servers.html'); - my $total_hosts = () = $response->{'content'} =~ /(\>Server )/g; - - for my $vm_name (sort keys %vms) { - my $_date = $vms{$vm_name}{'date'}; - my $_payment = $vms{$vm_name}{'payment'}; - my $_subscription = $vms{$vm_name}{'subscription'} || "no"; - my $_donated = $vms{$vm_name}{'donated'}; - my $_name = $vms{$vm_name}{'name'}; - my $_email = $vms{$vm_name}{'email'}; - my $_hostname = $vms{$vm_name}{'hostname'}; - - if ($_subscription =~ /no/) { next; } - if ($_donated =~ /done/) { next; } - if ($_donated =~ /expire/) { next; } - if ($_donated =~ /sponsor/) { next; } - if ($_donated =~ /renewal/) { next; } - if ($_date =~ /$year\//) { next; } - if ($_date =~ /\/$month\//) { - (my $_firstname, my $_lastname) = split(/ /, $_name, 2); - (my $_year, my $_month, my $_day) = split(/\//, $_date, 3); - my $ip4address = qx(grep -A2 $vm_name $_etc/dhcpd.conf | awk '/fixed-address/{print \$2}' | tr -d ';\n'); - my $ip6address = qx(grep -A3 $vm_name $_etc/dhcpd.conf | awk '/fixed-address-ipv6/{print \$2}' | tr -d ';\n'); - (my $ip6gw = $ip6address) =~ s/::.*/::1/; - - open(my $fh, '<', $template); - open my $fh_email, "|-", "/usr/sbin/sendmail -t"; - printf $fh_email "To: %s\n", $_email; - while (my $row = <$fh>) { - chomp $row; - $row =~ s/NAME/$_firstname/g; - $row =~ s/IPV4$/$ip4address/g; - $row =~ s/IPV4NETMASK$/$_ip4netmask/g; - $row =~ s/IPV4GW$/$_ip4gw/g; - $row =~ s/IPV6$/$ip6address/g; - $row =~ s/IPV6GW$/$ip6gw/g; - $row =~ s/VMID/$vm_name/g; - $row =~ s/SERVER/$_server/g; - - $row =~ s/YEAR/$year/g; - $row =~ s/TOTAL_DONATED/$total_donated/g; - $row =~ s/TOTAL_VMS/$total_vms/g; - $row =~ s/TOTAL_HOSTS/$total_hosts/g; - $row =~ s/PAYMENT/$_payment/g; - - if ($row =~ /TIME\((.*)\)/) { - my @TIMES = split(/,/, $1); - $row =~ s/TIME\(.*\)/$TIMES[$oddeven]/g; - } - - print $fh_email "$row\n"; - } - close $fh_email; - print "SUBSCRIPTION: $_date, $_payment, $_name, $_email, $_hostname, $_server ($vm_name), $ip4address\n"; - } - } -} - -# function to notify people -sub notify { - my %conf = %{$_[0]}; - my %vms = %{$_[1]}; - my $_etc = $conf{'conf'}{'ETC'}; - my $_vms = $conf{'conf'}{'VMS'}; - my $_tmpl = $conf{'conf'}{'TEMPLATES'}; - my $_server = $conf{'conf'}{'SERVER'}; - my $_ip4netmask = $conf{'conf'}{'NETMASK'}; - my $_ip4gw = $conf{'conf'}{'ROUTER'}; - - my $template = "$_tmpl/email-notify.txt"; - - my $year = strftime("%Y", localtime); - my $month = strftime("%m", localtime); - my $server_number = $1 if $_server =~ /([0-9]+)/; - my $oddeven = $server_number % 2; - my @stopped_vms = qx(vmctl show | grep stopped | awk '{print \$9}'); - - for my $vm_name (sort keys %vms) { - my $_date = $vms{$vm_name}{'date'}; - my $_payment = $vms{$vm_name}{'payment'}; - my $_donated = $vms{$vm_name}{'donated'}; - my $_name = $vms{$vm_name}{'name'}; - my $_email = $vms{$vm_name}{'email'}; - my $_hostname = $vms{$vm_name}{'hostname'}; - - if (-e "$template") { - (my $_firstname, my $_lastname) = split(/ /, $_name, 2); - my $ip4address = qx(grep -A2 $vm_name $_etc/dhcpd.conf | awk '/fixed-address/{print \$2}' | tr -d ';\n'); - my $ip6address = qx(grep -A3 $vm_name $_etc/dhcpd.conf | awk '/fixed-address-ipv6/{print \$2}' | tr -d ';\n'); - (my $ip6gw = $ip6address) =~ s/::.*/::1/; - - open(my $fh, '<', $template); - open my $fh_email, "|-", "/usr/sbin/sendmail -t"; - printf $fh_email "To: %s\n", $_email; - while (my $row = <$fh>) { - chomp $row; - $row =~ s/NAME/$_firstname/g; - $row =~ s/IPV4$/$ip4address/g; - $row =~ s/IPV4NETMASK$/$_ip4netmask/g; - $row =~ s/IPV4GW$/$_ip4gw/g; - $row =~ s/IPV6$/$ip6address/g; - $row =~ s/IPV6GW$/$ip6gw/g; - $row =~ s/VMID/$vm_name/g; - $row =~ s/SERVER/$_server/g; - - $row =~ s/YEAR/$year/g; - $row =~ s/TOTAL_DONATED/$total_donated/g; - $row =~ s/TOTAL_VMS/$total_vms/g; - $row =~ s/TOTAL_HOSTS/$total_hosts/g; - $row =~ s/PAYMENT/$_payment/g; - - if ($row =~ /TIME\((.*)\)/) { - my @TIMES = split(/,/, $1); - $row =~ s/TIME\(.*\)/$TIMES[$oddeven]/g; - } - - print $fh_email "$row\n"; - } - close $fh_email; - print "NOTIFIED: $_date, $_payment, $_name, $_email, $_hostname, $_server ($vm_name), $ip4address\n"; - } - } -} - -sub msg { - my %conf = %{$_[0]}; - my %vms = %{$_[1]}; - my $_etc = $conf{'conf'}{'ETC'}; - my $_vms = $conf{'conf'}{'VMS'}; - my $_tmpl = $conf{'conf'}{'TEMPLATES'}; - my $_server = $conf{'conf'}{'SERVER'}; - my $_ip4netmask = $conf{'conf'}{'NETMASK'}; - my $_ip4gw = $conf{'conf'}{'ROUTER'}; - - my $template = "$_tmpl/email-msg.txt"; - - my $year = strftime("%Y", localtime); - my $month = strftime("%m", localtime); - my $server_number = $1 if $_server =~ /([0-9]+)/; - my $oddeven = $server_number % 2; - my @stopped_vms = qx(vmctl show | grep stopped | awk '{print \$9}'); - - for my $vm_name (sort keys %vms) { - my $_date = $vms{$vm_name}{'date'}; - my $_payment = $vms{$vm_name}{'payment'}; - my $_donated = $vms{$vm_name}{'donated'}; - my $_name = $vms{$vm_name}{'name'}; - my $_email = $vms{$vm_name}{'email'}; - my $_hostname = $vms{$vm_name}{'hostname'}; - - if ($function_variable =~ /empty/) { print "No VMID provided\n"; last; } - if ($function_variable =~ /$vm_name/) { - (my $_firstname, my $_lastname) = split(/ /, $_name, 2); - my $ip4address = qx(grep -A2 $vm_name $_etc/dhcpd.conf | awk '/fixed-address/{print \$2}' | tr -d ';\n'); - my $ip6address = qx(grep -A3 $vm_name $_etc/dhcpd.conf | awk '/fixed-address-ipv6/{print \$2}' | tr -d ';\n'); - (my $ip6gw = $ip6address) =~ s/::.*/::1/; - - open(my $fh, '<', $template); - open my $fh_email, "|-", "/usr/sbin/sendmail -t"; - printf $fh_email "To: %s\n", $_email; - while (my $row = <$fh>) { - chomp $row; - $row =~ s/NAME/$_firstname/g; - $row =~ s/IPV4$/$ip4address/g; - $row =~ s/IPV4NETMASK$/$_ip4netmask/g; - $row =~ s/IPV4GW$/$_ip4gw/g; - $row =~ s/IPV6$/$ip6address/g; - $row =~ s/IPV6GW$/$ip6gw/g; - $row =~ s/VMID/$vm_name/g; - $row =~ s/SERVER/$_server/g; - - $row =~ s/YEAR/$year/g; - $row =~ s/TOTAL_DONATED/$total_donated/g; - $row =~ s/TOTAL_VMS/$total_vms/g; - $row =~ s/TOTAL_HOSTS/$total_hosts/g; - $row =~ s/PAYMENT/$_payment/g; - - if ($row =~ /TIME\((.*)\)/) { - my @TIMES = split(/,/, $1); - $row =~ s/TIME\(.*\)/$TIMES[$oddeven]/g; - } - - print $fh_email "$row\n"; - } - close $fh_email; - print "MSG: $_date, $_payment, $_name, $_email, $_hostname, $_server ($vm_name), $ip4address\n"; - } - } -} - -sub deprovision { - my %conf = %{$_[0]}; - my %vms = %{$_[1]}; - my $_etc = $conf{'conf'}{'ETC'}; - my $_vms = $conf{'conf'}{'VMS'}; - my $_tmpl = $conf{'conf'}{'TEMPLATES'}; - my $_server = $conf{'conf'}{'SERVER'}; - my $_ip4netmask = $conf{'conf'}{'NETMASK'}; - my $_ip4gw = $conf{'conf'}{'ROUTER'}; - - my $template = "$_tmpl/email-deprovision.txt"; - - my $year = strftime("%Y", localtime); - my $month = strftime("%m", localtime); - my $server_number = $1 if $_server =~ /([0-9]+)/; - my $oddeven = $server_number % 2; - my @stopped_vms = qx(vmctl show | grep stopped | awk '{print \$9}'); - - for my $vm_name (sort keys %vms) { - my $_date = $vms{$vm_name}{'date'}; - my $_payment = $vms{$vm_name}{'payment'}; - my $_subscription = $vms{$vm_name}{'subscription'} || "no"; - my $_donated = $vms{$vm_name}{'donated'}; - my $_name = $vms{$vm_name}{'name'}; - my $_email = $vms{$vm_name}{'email'}; - my $_hostname = $vms{$vm_name}{'hostname'}; - - if ($_subscription =~ /yes/) { next; } - if ($_donated =~ /done/) { next; } - if ($_donated =~ /expire/) { next; } - if ($_donated =~ /sponsor/) { next; } - if ($_donated =~ /renewal/) { next; } - if ($_date =~ /$year\//) { next; } - if ($_date =~ /\/$month\//) { - (my $_firstname, my $_lastname) = split(/ /, $_name, 2); - (my $_year, my $_month, my $_day) = split(/\//, $_date, 3); - my $ip4address = qx(grep -A2 $vm_name $_etc/dhcpd.conf | awk '/fixed-address/{print \$2}' | tr -d ';\n'); - my $ip6address = qx(grep -A3 $vm_name $_etc/dhcpd.conf | awk '/fixed-address-ipv6/{print \$2}' | tr -d ';\n'); - (my $ip6gw = $ip6address) =~ s/::.*/::1/; - - open(my $fh, '<', $template); - open my $fh_email, "|-", "/usr/sbin/sendmail -t"; - printf $fh_email "To: %s\n", $_email; - while (my $row = <$fh>) { - chomp $row; - $row =~ s/NAME/$_firstname/g; - $row =~ s/IPV4$/$ip4address/g; - $row =~ s/IPV4NETMASK$/$_ip4netmask/g; - $row =~ s/IPV4GW$/$_ip4gw/g; - $row =~ s/IPV6$/$ip6address/g; - $row =~ s/IPV6GW$/$ip6gw/g; - $row =~ s/VMID/$vm_name/g; - $row =~ s/SERVER/$_server/g; - - $row =~ s/YEAR/$year/g; - $row =~ s/TOTAL_DONATED/$total_donated/g; - $row =~ s/TOTAL_VMS/$total_vms/g; - $row =~ s/TOTAL_HOSTS/$total_hosts/g; - $row =~ s/PAYMENT/$_payment/g; - - if ($row =~ /TIME\((.*)\)/) { - my @TIMES = split(/,/, $1); - $row =~ s/TIME\(.*\)/$TIMES[$oddeven]/g; - } - - print $fh_email "$row\n"; - } - close $fh_email; - print "DEPROVISION: $_date, $_payment, $_name, $_email, $_hostname, $_server ($vm_name), $ip4address\n"; - } - } -} - -# function to print all keys & values for debug purposes -sub debug_parse { - my %conf = %{$_[0]}; - my %vms = %{$_[1]}; - my $_etc = $conf{'conf'}{'ETC'}; - my $_vms = $conf{'conf'}{'VMS'}; - my $_tmpl = $conf{'conf'}{'TEMPLATES'}; - my $_server = $conf{'conf'}{'SERVER'}; - my $_ip4netmask = $conf{'conf'}{'NETMASK'}; - my $_ip4gw = $conf{'conf'}{'ROUTER'}; - - my $template = "$_tmpl/email-debug.txt"; - - my $year = strftime("%Y", localtime); - my $month = strftime("%m", localtime); - my $server_number = $1 if $_server =~ /([0-9]+)/; - my $oddeven = $server_number % 2; - my @stopped_vms = qx(vmctl show | grep stopped | awk '{print \$9}'); - - print "All VMs\n##\n"; - for my $vm_name (sort keys %vms) { - my $_date = $vms{$vm_name}{'date'}; - my $_payment = $vms{$vm_name}{'payment'}; - my $_donated = $vms{$vm_name}{'donated'}; - my $_name = $vms{$vm_name}{'name'}; - my $_email = $vms{$vm_name}{'email'}; - my $_hostname = $vms{$vm_name}{'hostname'}; - print "$_date, $_payment, $_name, $_email, $_hostname, $_server ($vm_name)\n"; - } - print "##\n"; -} - # check if _deploy.conf exists my $dev = $ENV{'HOME'} . "/openbsd.amsterdam/deploy.pl"; my $prod = $ENV{'HOME'}; @@ -615,13 +159,41 @@ if (-d "$dev") { my @files = glob "$conf{'conf'}{'VMS'}/*.txt"; %vms = get_variables('vms', @files); -# run functions -if ($debug) { debug_parse(\%conf, \%vms); } -if ($function =~ /deployed/) { deployed(\%conf, \%vms); } -elsif ($function =~ /stopped/) { stopped(\%conf, \%vms); } -elsif ($function =~ /renewal/) { renewal(\%conf, \%vms); } -elsif ($function =~ /subscription/) { subscription(\%conf, \%vms); } -elsif ($function =~ /notify/) { notify(\%conf, \%vms); } -elsif ($function =~ /msg/) { msg(\%conf, \%vms); } -elsif ($function =~ /deprovision/) { deprovision(\%conf, \%vms); } -else { print "Specify function: deployed, stopped, renewal, notify, deprovision\n"; } +if ($function =~ /notify/) { + mailout(\%conf, \%vms); +} + +if ($function =~ /(msg|deployed)/ and $function_variable !~ /empty/) { + my %slice = %vms{$function_variable}; + mailout(\%conf, \%slice); +} + +if ($function =~ /(renewal|subscription|deprovision)/) { + my $year = strftime("%Y", localtime); + my $month = strftime("%m", localtime); + for my $vm_name (sort keys %vms) { + if ($vms{$vm_name}{'donated'} =~ /(done|expire|sponsor|renewal)/) { delete $vms{$vm_name}; next; } + if ($vms{$vm_name}{'date'} =~ /$year\//) { delete $vms{$vm_name}; next; } + if ($vms{$vm_name}{'date'} !~ /\/$month\//) { delete $vms{$vm_name}; next; } + if ($function =~ /(renewal|deprovision)/) { + if (defined $vms{$vm_name}{'subscription'} and $vms{$vm_name}{'subscription'} =~ /yes/) { delete $vms{$vm_name}; next; } + } + if ($function =~ /subscription/) { + if (defined $vms{$vm_name}{'subscription'} and $vms{$vm_name}{'subscription'} eq "") { delete $vms{$vm_name}; next; } + } + + } + mailout(\%conf, \%vms); +} + +if ($function =~ /stopped/) { + my @stopped_vms = qx(vmctl show | grep stopped | awk '{print \$9}'); + for my $vm_name (sort keys %vms) { + if (!grep(/$vm_name/, @stopped_vms)) { delete $vms{$vm_name}; next; } + } + mailout(\%conf, \%vms); +} + +if ($function =~ /empty/) { + print "Specify function: deployed , stopped, renewal, notify, deprovision, msg \n"; +} diff --git a/vms/vm01.txt b/vms/vm01.txt index bac8635..3ac641f 100644 --- a/vms/vm01.txt +++ b/vms/vm01.txt @@ -1,7 +1,7 @@ mac="fe:e1:ab:dd:73:a4" ip="192.168.1.1" owner="testuser1" -date="2018/02/02" +date="2018/05/02" payment="70" donated="" name="Test1 User"