Community discussions

MikroTik App
 
shahani
newbie
Topic Author
Posts: 26
Joined: Wed Jan 02, 2019 11:29 pm

Log monitor script

Fri Mar 13, 2020 1:01 pm

Greetings,

I've set up a script that notifies me of failed connect attempts (wrong passwords) Via E-mail, SMS and Telegram instant-messages.
I edited and ran the script from mikrotik wiki on log monitoring and alert systems at :

https://wiki.mikrotik.com/wiki/Monitor_ ... run_script

It's running smoothly on my switch which is my CAPsMAN. it sends a message immediately after an attempt. The issue is it sends 2 more redundant messages once at 00:00 local time ( GMT +3:30 Tehran) and once at 00:00 GMT .

Would you be so kind to review my code and tell me how I can make it so I'll only receive one alert immediately and no more redundant messages.

Thanks in advance.
# BEGIN SETUP
:local scheduleName "Wifi-LoginFailure"
:local startBuf [:toarray [/log find  message~"4-way handshake"]]
:local removeThese 
#Email
:local emailAddress "Email@gmail.com"
#SMS-Info
:local SmsUsername "Username"
:local SmsPassword "Password"
:local SendMobileNum "5000221000"
:local AliNum "09012223344"
:local SmsMsg 
:local SmsMsgFix
#Telegram-Info
:local BotToken "720638877:AAFwcEy5n5TN130yVFMIeQ7zG-XXXXXXXXX"
:local AliID "136000000"
:local TelOutput
#Msg Count
:local Msgcounter 0
# END SETUP

# warn if schedule does not exist
:if ([:len [/system scheduler find name="$scheduleName"]] = 0) do={
  /log warning "[LOGMON] ERROR: Schedule does not exist. Create schedule and edit script to match name"
}

# get last time
:local lastTime [/system scheduler get [find name="$scheduleName"] comment]
# for checking time of each log entry
:local currentTime
# log message
:local message
 
# final output
:local output

:local keepOutput false
# if lastTime is empty, set keepOutput to true
:if ([:len $lastTime] = 0) do={
  :set keepOutput true
}


:local counter 0
# loop through all log entries that have been found
:foreach i in=$startBuf do={
 
# loop through all removeThese array items
  :local keepLog true
  :foreach j in=$removeThese do={
#   if this log entry contains any of them, it will be ignored
    :if ([/log get $i message] ~ "$j") do={
      :set keepLog false
    }
  }
  :if ($keepLog = true) do={
   
   :set message [/log get $i message]

#   LOG DATE
#   depending on log date/time, the format may be different. 3 known formats
#   format of jan/01/2002 00:00:00 which shows up at unknown date/time. Using as default
    :set currentTime [ /log get $i time ]
#   format of 00:00:00 which shows up on current day's logs
   :if ([:len $currentTime] = 8 ) do={
     :set currentTime ([:pick [/system clock get date] 0 11]." ".$currentTime)
    } else={
#     format of jan/01 00:00:00 which shows up on previous day's logs
     :if ([:len $currentTime] = 15 ) do={
        :set currentTime ([:pick $currentTime 0 6]."/".[:pick [/system clock get date] 7 11]." ".[:pick $currentTime 7 15])
      }
   }
    
#   if keepOutput is true, add this log entry to output
   :if ($keepOutput = true) do={
     :set output ($output.$currentTime." ".$message."\r\n")

    set Msgcounter ($Msgcounter + 1)
    if ($Msgcounter < 11 ) do={
#   Only First 10 Message For Telegram
     :set TelOutput ($TelOutput.$currentTime." - ".$message."%0A")
    }

    if ($Msgcounter < 4 ) do={
#   Only First 3 Message For SMS
     :set SmsMsg ($SmsMsg.$currentTime." - ".$message."%0A")
    }
   }
#   if currentTime = lastTime, set keepOutput so any further logs found will be added to output
#   reset output in the case we have multiple identical date/time entries in a row as the last matching logs
#   otherwise, it would stop at the first found matching log, thus all following logs would be output
    :if ($currentTime = $lastTime) do={
     :set keepOutput true
     :set output ""
     :set TelOutput ""
     :set SmsMsg ""
   }
  }

#   if this is last log entry
  :if ($counter = ([:len $startBuf]-1)) do={
#   If keepOutput is still false after loop, this means lastTime has a value, but a matching currentTime was never found.
#   This can happen if 1) The router was rebooted and matching logs stored in memory were wiped, or 2) An item is added
#   to the removeThese array that then ignores the last log that determined the lastTime variable.
#   This resets the comment to nothing. The next run will be like the first time, and you will get all matching logs
   :if ($keepOutput = false) do={
#     if previous log was found, this will be our new lastTime entry      
     :if ([:len $message] > 0) do={
        :set output ($output.$currentTime." ".$message."\r\n")
     
     :set Msgcounter ($Msgcounter + 1)
#   Last Message For Telegram
     :set TelOutput ($TelOutput.$currentTime." - ".$message."%0A")

#   Last Message For SMS
     :set SmsMsg ($SmsMsg.$currentTime." - ".$message."%0A")
      }
    }
  }
  :set counter ($counter + 1)
}

# If we have output, save new date/time, and send email
if ([:len $output] > 0) do={
  /system scheduler set [find name="$scheduleName"] comment=$currentTime

  /log info "Start Sending Email, Telegram and SMS Message For Wifi Login Failure Alert"

  /tool e-mail send to="$emailAddress"  subject="WIFI Login Failure Alert" body="WIFI Login Failure Alert - $currentTime \r\n $output"

  /tool fetch url="https://api.telegram.org/bot$BotToken/sendmessage?chat_id=$AliID&text=WIFI Login Failure Alert :%0A(Run : $currentTime)%0A$TelOutput" mode=https keep-result=no 

 :set SmsMsg ("WIFI%20Login%20Failure%20:%0A(Run%20:%20".$currentTime.")%0A".$SmsMsg)
# Replace Spase With %20 For Sms String
:if ([:find $SmsMsg " " -1] > 0) do={
 :for i from=0 to=([:len $SmsMsg] -1) step=1 do={
  :local actualchar value=[:pick $SmsMsg $i];
  :if ($actualchar = " ") do={ :set actualchar value="%20" };
  :set SmsMsgFix value=($SmsMsgFix.$actualchar);
 }
}

  /tool fetch ascii=yes mode=https keep-result=no url="https://raygansms.com/SendMessageWithUrl.ashx?Username=$SmsUsername&Password=$SmsPassword&PhoneNumber=$SendMobileNum&MessageBody=$SmsMsgFix&RecNumber=$AliNum&Smsclass=1" 

  /log info "[LOGMON] New logs found, WIFI Login Failure, sended alert"
}
CCR1036-12G-4S-149 | CRS328_24p_4s_rm | RB4011iGS+5HacQ2HnD-IN | RB2011UiAS-2HnD-IN | RB952Ui-5ac2nD
 
sopyan0807
newbie
Posts: 40
Joined: Wed Jan 22, 2020 5:21 pm
Location: Indonesia
Contact:

Re: Log monitor script

Tue Apr 07, 2020 12:07 pm

You need to add a delay between the two commands, for example
  /tool e-mail send to="$emailAddress"  subject="WIFI Login Failure Alert" body="WIFI Login Failure Alert - $currentTime \r\n $output"
 :delay 5;
  /tool fetch url="https://api.telegram.org/bot$BotToken/sendmessage?chat_id=$AliID&text=WIFI Login Failure Alert :%0A(Run : $currentTime)%0A$TelOutput" mode=https keep-result=no 
hope this works
 
shahani
newbie
Topic Author
Posts: 26
Joined: Wed Jan 02, 2019 11:29 pm

Re: Log monitor script

Sat Apr 11, 2020 11:58 pm

You need to add a delay between the two commands
hope this works
Thank you, I've added a delay but the problem persists.
...
  /tool e-mail send to="$emailAddress"  subject="WIFI Login Failure Alert" body="WIFI Login Failure Alert - $currentTime \r\n $output"
:delay 15;
  /tool fetch url="https://api.telegram.org/bot$BotToken/sendmessage?chat_id=$AliID&text=WIFI Login Failure Alert :%0A(Run : $currentTime)%0A$TelOutput" mode=https keep-result=no 
...
:delay 15;
  /tool fetch ascii=yes mode=https keep-result=no url="https://raygansms.com/SendMessageWithUrl.ashx?Username=$SmsUsername&Password=$SmsPassword&PhoneNumber=$SendMobileNum&MessageBody=$SmsMsgFix&RecNumber=$AliNum&Smsclass=1" 
  ...
CCR1036-12G-4S-149 | CRS328_24p_4s_rm | RB4011iGS+5HacQ2HnD-IN | RB2011UiAS-2HnD-IN | RB952Ui-5ac2nD
 
dimension
just joined
Posts: 1
Joined: Sun Oct 18, 2020 9:35 pm
Location: Ukraine

Re: Log monitor script  [SOLVED]

Sun Oct 18, 2020 9:39 pm

In this script the time is fixed.
# BEGIN SETUP
:local scheduleName "LogMikrotik"
:local bot "BOT TELEGRAM ANDA"
:local ChatID "CHAT ID ANDA"
:local startBuf [:toarray [/log find message~"logged in" || topics~"wireless" || topics~"error" || topics~"warning" || topics~"critical"]]
:local removeThese {"telnet"}
:local TimeOFF [:toarray ("00:00","03:00")]
# END SETUP

if ([:len [/system scheduler find name="$scheduleName"]] = 0) do={
/log warning "[LOGMON] ERROR: Schedule does not exist. Create schedule and edit script to match name"
}

:local name [/system  identity  get name]
:local TimeNow [:pick [/system clock get time] 0 5]
:local lastTime [/system scheduler get [find name="$scheduleName"] comment]
:local currentTime
:local message
:local output
:local keepOutput false
:local logTrue

if ([:len $lastTime] = 0) do={
:set keepOutput true
}

:foreach i in=$startBuf do={
  :local var true
  :foreach j in=$removeThese do={
    if ([/log get $i message] ~ $j) do={
      :set var false
      }
    }
  if ($var=true) do={:set logTrue ($logTrue, $i)}
  }
  
:foreach l in=$logTrue do={
  :set currentTime [ /log get $l time ]

  if ([:len $currentTime] = 8 ) do={
    :set currentTime ([:pick [/system clock get date] 0 11]." ".$currentTime)
    } else={

    if ([:len $currentTime] = 15 ) do={
      :set currentTime ([:pick $currentTime 0 6]."/".[:pick [/system clock get date] 7 11]." ".[:pick $currentTime 7 15])
      }
    }
  if ($keepOutput=true) do={
    :set message [/log get $l message]
    if ([/log get $l message] ~ "[0-F][0-F]:[0-F][0-F]:[0-F][0-F]:[0-F][0-F]:[0-F][0-F]:[0-F][0-F]") do={
      :foreach k in=[/ip dhcp-server lease find] do={
        :local mac [/ip dhcp-server lease get $k value-name=mac-address]
        if ([/log get $l message] ~ "$mac") do={
          :set message ($message . " / " . [/ip dhcp-server lease get $k value-name=comment])
          }
        }
      }
  :set output ($output.$currentTime." ".$message."%0A%0A")
    } else={

    :set message [/log get $l message]
  
    if ($currentTime = $lastTime) do={
      :set keepOutput true
      }
    }
  }
if (([:len $output] > 0) and ($TimeOFF ~ $TimeNow)) do={
  /system scheduler set [find name="$scheduleName"] comment=$currentTime
  } else={

  if (([:len $output] > 0) and ($currentTime != $lastTime)) do={
    /system scheduler set [find name="$scheduleName"] comment=$currentTime
    /tool fetch url="https://api.telegram.org/bot$bot/sendmessage?chat_id=$ChatID&text= $name : %0A$output" keep-result=no;
    } else={

    if (([:len $message] > 0) and ($currentTime != $lastTime)) do={
      /system scheduler set [find name="$scheduleName"] comment=$currentTime
      }
    }
  }
https://forummikrotik.ru/viewtopic.php? ... da0#p70882

and this:
# script by Virtue
:delay 10
:local timeNow [/system clock get time]
:local logs
:if ([:pick $timeNow 0 2] < 03) do={
:local logAll [/log find]
:local date [/system clock get date]
:foreach h in=$logAll do={
:local htime [/log get $h time]
:if (($htime ~ [:pick $date 0 6]) and ([:pick $htime 7 15] > ($timeNow - 00:01:00))) do={
:set logs ($logs, $h)
  }
 }
} else={
:set logs [/log find time >= ($timeNow - 00:01:00)]
}
:if ([:len $logs] > 0 ) do={
:local messages
:foreach i in=$logs do={
:local imessage [/log get $i message]
:if ($imessage ~"telnet" and $imessage ~"logged in") do={} else={
:local topics [/log get $i topics]
:if ($topics ~"wireless" || $topics ~"warning" || $imessage ~"logged in" || $topics ~"error" || $topics ~"critical") do={
:if ($imessage ~ "[0-F][0-F]:[0-F][0-F]:[0-F][0-F]:[0-F][0-F]:[0-F][0-F]:[0-F][0-F]") do={
:foreach k in=[/ip dhcp-server lease find] do={
:local mac [/ip dhcp-server lease get $k value-name=mac-address]
:if ($imessage ~ "$mac") do={
:set imessage ($imessage . " / " . [/ip dhcp-server lease get $k value-name=comment])
      }
     }
    }
:local currentTime [ /log get $i time ]
:set messages ($messages.$currentTime." ".$imessage."%0A%0A")
   }
  }
 }
:if ([:len $messages] > 0 ) do={
:local botID "bot1000075423:AAEYn0eWtjTRKKg_BGF7fQiY5UjrTsdMfhl"
:local chatID "-1001422750080"
:local mikname [/system  identity  get name]
/tool fetch keep-result=no url="https://api.telegram.org/$botID/sendmessage?chat_id=$chatID&text= $mikname : %0A$messages"
 }
}
https://forummikrotik.ru/viewtopic.php?p=73994#p73994
 
User avatar
eworm
Forum Veteran
Forum Veteran
Posts: 749
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Log monitor script

Mon Oct 19, 2020 10:51 pm

I think my script log-forward could serve your needs... Though it does not only notify about failed login attempts but everything interesting - configurable with filters.
It depends on other scripts, see the main README on how to install this.
Manage RouterOS scripts and extend your devices' functionality: RouterOS Scripts
 
shahani
newbie
Topic Author
Posts: 26
Joined: Wed Jan 02, 2019 11:29 pm

Re: Log monitor script

Thu Nov 26, 2020 12:39 pm

thank you dimension, fixed.
# script by Virtue
#Logs Monitor Script
:delay 10
:local timeNow [system clock get time]
:local date [system clock get date]
:local logs
#time zone difference to GMT time=+04:30
:if ([:pick $timeNow 0 2] < 05) do={
	:local logAll [log find]
	:foreach h in=$logAll do={
		:local htime [log get $h time]
		#Schedule Intereval=00:04:00
		:if (($htime ~ [:pick $date 0 6]) and ([:pick $htime 7 15] > ($timeNow - 00:04:00))) do={
			:set logs ($logs, $h)
		}
	}
} else={
	#Schedule Intereval=00:04:00
	:set logs [log find time >= ($timeNow - 00:04:00)]
}
:if ([:len $logs] > 0 ) do={
:local EmailMsg
:local TelegramMsg
:local SmsMsg
:local Msgcounter 0
:local finded false
:local mac
:local comment
	:foreach i in=$logs do={
	:local imessage [log get $i message]
		:if ($imessage ~"telnet" and $imessage ~"logged in") do={} else={
			:local topics [log get $i topics]
			:if ($imessage ~"4-way handshake" || $imessage ~"banned" || $imessage ~"login failure" || $imessage ~"authentication failed" || $imessage ~">: connected" || $imessage ~"logged in" ) do={
				:set finded false
				:if ($imessage ~ "[0-F][0-F]:[0-F][0-F]:[0-F][0-F]:[0-F][0-F]:[0-F][0-F]:[0-F][0-F]") do={
					:if ($finded = false) do={
						:foreach k in=[ip dhcp-server lease find] do={
							:set mac [ip dhcp-server lease get $k value-name=mac-address]
							:if ($imessage ~ "$mac") do={
								:set comment [ip dhcp-server lease get $k value-name=comment]
								:if ($comment != "") do={
									:set imessage ($imessage . " / " . $comment)
									:set finded true
								}
							}
						}
					}
					:if ($finded = false) do={
						:foreach h in=[ip hotspot ip-binding find] do={
							:set mac [ip hotspot ip-binding get $h value-name=mac-address]
							:if ($imessage ~ "$mac") do={
								:set comment [ip hotspot ip-binding get $h value-name=comment]
								:if ($comment != "") do={
									:set imessage ($imessage . " / " . $comment)
									:set finded true
								}
							}
						}
					}
					:if ($finded = false) do={
						:foreach w in=[interface wireless access-list find] do={
							:set mac [interface wireless access-list get $w value-name=mac-address]
							:if ($imessage ~ "$mac") do={
								:set comment [interface wireless access-list get $w value-name=comment]
								:if ($comment != "") do={
									:set imessage ($imessage . " / " . $comment)
									:set finded true
								}
							}
						}
					}					
					:if ($finded = false) do={
						:foreach c in=[caps-man access-list find] do={
							:set mac [caps-man access-list get $c value-name=mac-address]
							:if ($imessage ~ "$mac") do={
								:set comment [caps-man access-list get $c value-name=comment]
								:if ($comment != "") do={
									:set imessage ($imessage . " / " . $comment)
									:set finded true
								}
							}
						}
					}
				}
				:local currentTime ([:pick $date 0 11]." ".[ log get $i time ])				
				:set EmailMsg ($EmailMsg.$currentTime." ".$imessage."\r\n\r\n")
				:set Msgcounter ($Msgcounter + 1)
				if ($Msgcounter < 11 ) do={
				 #Only First 10 Logs For Telegram
				 :set TelegramMsg ($TelegramMsg.$currentTime." - ".$imessage."%0A%0A")
				}
				if ($Msgcounter < 4 ) do={
				 #Only First 3 Logs For SMS
				 :set SmsMsg ($SmsMsg.$currentTime."%20-%20".$imessage."%0A%0A")
				}
			}
		}
	}
	:if ([:len $EmailMsg] > 0 ) do={
		:local systemName [system identity get name]
		:local emailAddress "MyEmail@gmail.com"
		 log info "[LOGMON] New logs found, Logs Monitor Script, trying to send alert."
		 :do {
		   tool e-mail send to="$emailAddress"  subject="[$systemName] LOGMON Alert!" body="[$systemName] Logs Monitor Alert: \r\n $EmailMsg"
		   log info "[LOGMON] Email Sended."
		  } on-error={
		   log error "[LOGMON] Failed to send email.";
		  }		  
		:local BotToken "BotToken"
		:local MyID "Telegram Number ID"
		:local TelMsgFix
		:local achar
		  :set TelegramMsg ("[$systemName] Logs Monitor Alert :%0A".$TelegramMsg)
			 # Replace " H" With " h" For Telegram String		 
			:if ([:find $TelegramMsg " H" -1] > 0) do={
			set achar ""
			set TelMsgFix ""
			 :for i from=0 to=([:len $TelegramMsg] -1) step=1 do={
			  :set achar value=[:pick $TelegramMsg $i]
				  :if ($achar = "H") do={
				   :if ([:pick $TelegramMsg ($i - 1) ($i + 1)] = " H") do={
					:set achar value="h"
				   }
				  };
			  :set TelMsgFix value=($TelMsgFix.$achar)
			 }
			} else={:set TelMsgFix value=($TelegramMsg)}		
		 :do {
		   tool fetch url="https://api.telegram.org/bot$BotToken/sendmessage?chat_id=$MyID&text=$TelMsgFix" mode=https keep-result=no 
		   log info "[LOGMON] Telegram message Sended."
		  } on-error={
		   log error "[LOGMON] Failed to send Telegram message.";
		  }
		:local SmsUsername "Username"
		:local SmsPassword "Password"
		:local SendMobileNum "50000000000082"
		:local MyNum "0901XXXXXXX"
		:local SmsMsgFix
		:local actualchar  
			 # Replace (Spase With %20) and ("<" With "[") and (">" With "]") For Sms String
			:if ( ([:find $SmsMsg " " -1] > 0) || ([:find $SmsMsg "<" -1] > 0) || ([:find $SmsMsg ">" -1] > 0) ) do={
			   :set actualchar ("")   
			   :set SmsMsgFix ("") 
			   :for i from=0 to=([:len $SmsMsg] -1) step=1 do={
				  :set actualchar value=[:pick $SmsMsg $i]
					  :if ($actualchar = " ") do={ 
						:set actualchar value="%20" 
					  } else={
							:if ($actualchar = "<") do={ 
							  :set actualchar value="[" 
							} else={
								:if ($actualchar = ">") do={ :set actualchar value="]" }
							}
					  }
				  :set SmsMsgFix value=($SmsMsgFix.$actualchar)
				}
			} else={:set SmsMsgFix value=($SmsMsg)}	
		 :do {
			tool fetch ascii=yes mode=https keep-result=no url="https://raygansms.com/SendMessageWithUrl.ashx?Username=$SmsUsername&Password=$SmsPassword&PhoneNumber=$SendMobileNum&MessageBody=$SmsMsgFix&RecNumber=$MyNum&Smsclass=1" 
			log info "[LOGMON] SMS Sended."
		  } on-error={
			log error "[LOGMON] Failed to send SMS.";
		  } 
	}
}
Instead of replacing " H" With " h" For Telegram String we could replace " " with "%20"
# script by Virtue
#Logs Monitor Script
:delay 10
:local timeNow [system clock get time]
:local date [system clock get date]
:local logs
#time zone difference to GMT time=+04:30
:if ([:pick $timeNow 0 2] < 05) do={
	:local logAll [log find]
	:foreach h in=$logAll do={
		:local htime [log get $h time]
		#Schedule Intereval=00:01:00
		:if (($htime ~ [:pick $date 0 6]) and ([:pick $htime 7 15] > ($timeNow - 00:01:00))) do={
			:set logs ($logs, $h)
		}
	}
} else={
	#Schedule Intereval=00:01:00
	:set logs [log find time >= ($timeNow - 00:01:00)]
}
:if ([:len $logs] > 0 ) do={
:local EmailMsg
:local TelegramMsg
:local SmsMsg
:local Msgcounter 0
:local finded false
:local mac
:local comment
	:foreach i in=$logs do={
	:local imessage [log get $i message]
	:local topics [log get $i topics]
		:if ( ($imessage ~"telnet" && $imessage ~"logged in") || ($topics ~"hotspot") ) do={} else={		
			:if ($imessage ~"4-way handshake" || $imessage ~"banned" || $imessage ~"login failure" || $imessage ~"authentication failed" || $imessage ~">: connected" || $imessage ~"logged in" ) do={
				:set finded false
				:if ($imessage ~ "[0-F][0-F]:[0-F][0-F]:[0-F][0-F]:[0-F][0-F]:[0-F][0-F]:[0-F][0-F]") do={
					:if ($finded = false) do={
						:foreach k in=[ip dhcp-server lease find] do={
							:set mac [ip dhcp-server lease get $k value-name=mac-address]
							:if ($imessage ~ "$mac") do={
								:set comment [ip dhcp-server lease get $k value-name=comment]
								:if ($comment != "") do={
									:set imessage ($imessage . " / " . $comment)
									:set finded true
								}
							}
						}
					}
					:if ($finded = false) do={
						:foreach h in=[ip hotspot ip-binding find] do={
							:set mac [ip hotspot ip-binding get $h value-name=mac-address]
							:if ($imessage ~ "$mac") do={
								:set comment [ip hotspot ip-binding get $h value-name=comment]
								:if ($comment != "") do={
									:set imessage ($imessage . " / " . $comment)
									:set finded true
								}
							}
						}
					}
					:if ($finded = false) do={
						:foreach w in=[interface wireless access-list find] do={
							:set mac [interface wireless access-list get $w value-name=mac-address]
							:if ($imessage ~ "$mac") do={
								:set comment [interface wireless access-list get $w value-name=comment]
								:if ($comment != "") do={
									:set imessage ($imessage . " / " . $comment)
									:set finded true
								}
							}
						}
					}					
					:if ($finded = false) do={
						:foreach c in=[caps-man access-list find] do={
							:set mac [caps-man access-list get $c value-name=mac-address]
							:if ($imessage ~ "$mac") do={
								:set comment [caps-man access-list get $c value-name=comment]
								:if ($comment != "") do={
									:set imessage ($imessage . " / " . $comment)
									:set finded true
								}
							}
						}
					}
				}
				:local currentTime ([:pick $date 0 11]." ".[ log get $i time ])				
				:set EmailMsg ($EmailMsg.$currentTime." ".$imessage."\r\n\r\n")
				:set Msgcounter ($Msgcounter + 1)
				if ($Msgcounter < 11 ) do={
				 #Only First 10 Logs For Telegram
				 :set TelegramMsg ($TelegramMsg.$currentTime." - ".$imessage."%0A%0A")
				}
				if ($Msgcounter < 4 ) do={
				 #Only First 3 Logs For SMS
				 :set SmsMsg ($SmsMsg.$currentTime."%20-%20".$imessage."%0A%0A")
				}
			}
		}
	}
	:if ([:len $EmailMsg] > 0 ) do={
		:local systemName [system identity get name]
		:local emailAddress "MyEmail@gmail.com"
		 log info "[LOGMON] New logs found, Logs Monitor Script, trying to send alert."
		 :do {
		   tool e-mail send to="$emailAddress"  subject="[$systemName] LOGMON Alert!" body="[$systemName] Logs Monitor Alert: \r\n $EmailMsg"
		   log info "[LOGMON] Email Sended."
		  } on-error={
		   log error "[LOGMON] Failed to send email.";
		  }		  
		:local BotToken "BotToken"
		:local MyID "Telegram Number ID"
		:local TelMsgFix
		:local achar
		  :set TelegramMsg ("[$systemName]%20Logs%20Monitor%20Alert%20:%0A".$TelegramMsg)
			 # Replace " " With "%20" For Telegram String		 
			:if ([:find $TelegramMsg " " -1] > 0) do={
			set achar ""
			set TelMsgFix ""
			 :for i from=0 to=([:len $TelegramMsg] -1) step=1 do={
				:set achar value=[:pick $TelegramMsg $i]
				:if ($achar = " ") do={ :set achar value="%20" }
				:set TelMsgFix value=($TelMsgFix.$achar)
			 }
			} else={:set TelMsgFix value=($TelegramMsg)}			
		 :do {
		   tool fetch url="https://api.telegram.org/bot$BotToken/sendmessage?chat_id=$MyID&text=$TelMsgFix" mode=https keep-result=no 
		   log info "[LOGMON] Telegram message Sended."
		  } on-error={
		   log error "[LOGMON] Failed to send Telegram message.";
		  }
		:local SmsUsername "Username"
		:local SmsPassword "Password"
		:local SendMobileNum "50000000000082"
		:local MyNum "0901XXXXXXX"
		:local SmsMsgFix
		:local actualchar  
			 # Replace (Spase With %20) and ("<" With "[") and (">" With "]") For Sms String
			:if ( ([:find $SmsMsg " " -1] > 0) || ([:find $SmsMsg "<" -1] > 0) || ([:find $SmsMsg ">" -1] > 0) ) do={
			   :set actualchar ("")   
			   :set SmsMsgFix ("") 
			   :for i from=0 to=([:len $SmsMsg] -1) step=1 do={
				  :set actualchar value=[:pick $SmsMsg $i]
					  :if ($actualchar = " ") do={ 
						:set actualchar value="%20" 
					  } else={
							:if ($actualchar = "<") do={ 
							  :set actualchar value="[" 
							} else={
								:if ($actualchar = ">") do={ :set actualchar value="]" }
							}
					  }
				  :set SmsMsgFix value=($SmsMsgFix.$actualchar)
				}
			} else={:set SmsMsgFix value=($SmsMsg)}	
		 :do {
			tool fetch ascii=yes mode=https keep-result=no url="https://raygansms.com/SendMessageWithUrl.ashx?Username=$SmsUsername&Password=$SmsPassword&PhoneNumber=$SendMobileNum&MessageBody=$SmsMsgFix&RecNumber=$MyNum&Smsclass=1" 
			log info "[LOGMON] SMS Sended."
		  } on-error={
			log error "[LOGMON] Failed to send SMS.";
		  } 
	}
}
CCR1036-12G-4S-149 | CRS328_24p_4s_rm | RB4011iGS+5HacQ2HnD-IN | RB2011UiAS-2HnD-IN | RB952Ui-5ac2nD

Who is online

Users browsing this forum: cashwu and 44 guests