Scenario:
We have a routerboard which have 2 wan pppoe-outX dialers. Gmail account is configured to send alerts. On the same LAN we have KANNEL sms gateway server which acts as a central sms gateway for sending receiving sms.
Disclaimer:
The script is designed for some specific network, it may not fit general public requirements, but still its a good idea to collect various scripting ideas for learning purposes and it may help in other tasks as well.
Requirements:
- If the Router is rebooted , it should send us Email and SMS with the new WAN ip addresses.
- It should check for both WAN connections status before acquiring IP addresses, if it won’t check for interface status and the dialer aren’t connected, the script will terminate, therefore this check must be added
- It should check for UPTIME , if the UPTIME is less than X Minutes, then it should consider the RB is actually rebooted, this check was required to prevent false detection of reboot. In some ROS, it was a bug that the RB doesn’t gets rebooted but reload the OS and the scripts consider that the RB got rebooted while it actually dont. so this check need to be added.
- Make sure you have already configured the /tools/emails section in RB to make email alerts work.
the SCRIPT !
use the following script, modify it as required. schedule it to run on system reboot only,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# Mikrotik reboot alert / UPTIME CHECK SCRIPT, with prevention of sending FALSE ALARM with optional Email and SMS Alert # We are using local KANNEL as SMS gateway and GMAIL as mail relay server # By Lupael # https://dev.i4e.com.bd # Email : support@i4e.com.bd # Get Uptime :local UPTIME [/system resource get uptime] # Set UPTIME Limit :local UPTIMELIMIT "00:05:00" # SET DATE TIME :local date; :local time; :set date [/system clock get date]; :set time [/system clock get time]; # if uptime is less then uptime limit threshold value, then consider router is actually rebooted, and take action / Lupael :if ($UPTIME<$UPTIMELIMIT) do={ :log error "ALERT: Router was rebooted just before $UPTIMELIMIT Minutes therefore sending Reboot SMS / Email Alert"; # Sleep , this is added so that RB and the KANNEL services may start properly / Lupael :delay 180s # GMAIL Setup :local gmailid "support@i4e.com.bd" :local GMAILPASS "uaahgksdgxizgckg" :global gmailsmtp :set gmailsmtp [:resolve "smtp.gmail.com"]; :local COMPANY "i4E" # KANNEL SMS Configuration #If you dont have kannel sms gateway ignore this. :local KURL "192.168.100.1" :local KID "kannel" :local KPASS "kannelpassword" #Mobile numbers of Admin :local cell1 "01711928149" :local cell2 "0171xxxxxxx" :global WAN1IP :global WAN2IP # in this RB,we have two pppoe-outx wan dialers, Check if dialer is present and connected, # this check is added because if dialer is not connected script was terminating, so this check is added now if ([/interface get pppoe-out1 disabled] = yes) do={ :log error "pppoe-out1 Interface disabled" }\ else={[:global WAN1IP [/ip address get [find where interface=pppoe-out1] address];]} if ([/interface get pppoe-out2 disabled] = yes) do={ :log error "pppoe-out2 Interface disabled" }\ else={[:global WAN2IP [/ip address get [find where interface=pppoe-out2] address];]} # email recipients, Set your email where you want to receive the alert :local mailsendto :set mailsendto info@i4e.com.bd :local mailsendto2 :set mailsendto2 chairman@speedlinksbd.com # Set Email Subject :local es "$[/system identity get name] $[/system clock get date] $[/system clock get time] $COMPANY MIKROTIK got rebooted ! new ips $WAN1IP $WAN2IP and Uptime is $UPTIME" # Set Email Body :local eb "$[/system identity get name] $[/system clock get date] $[/system clock get time] $COMPANY MIKROTIK got rebooted ! \nNew ip address are \n WAN1IP = $WAN1IP \n WAN2IP = $WAN2IP \n\n Current Uptime is $UPTIME \n\n This script is made by Ashik Iqbal Lupael !" # Finally send email to both amind email addresses /tool e-mail send to=$mailsendto subject=$es start-tls=yes body=$eb password=$GMAILPASS /tool e-mail send to=$mailsendto2 subject=$es body=$eb start-tls=yes password=$GMAILPASS :log warning "Email Done! for REBOOT ..." :log warning "SENDING SMS FOR REBOOT ALERT VIA KANNEL RADIUS GATEWAY ." # Send SMS using local KANNEL sms gateway /tool fetch url="http://$KHOST:13013/cgi-bin/sendsms\?username=$KID&password=$KPASS&to=$cell1+$cell2&text=$COMPANY+MIKROTIK+Router+was+rebooted+and+now+restored+at+$date+$time+and+new+ips+are+$WAN1IP+$WAN2IP+++++++[$COMPANY+Pvt+Ltd]" # if uptime is above then uptime limit threshold value, then no need to send SMS, this is to prevent false alarm. } else={ :log error "System is above then $UPTIMELIMIT, so no need to send reboot sms/email alert!" } # Script , Ends Here # Ashik Iqbal Lupael # support@i4e.com.bd |
Results~
Comments
0 comments
Please sign in to leave a comment.