Categories
GNU/Linux

GNU/Linux Telegram/Bot Mail Alert Script (Procmail/Python)

Small script to send a telegram chat message to a user when new mail arives. Script uses procmail and a small python script to process the message and to send the alert alert message through a telegram bot api.

.procmailrc:#LOGFILE=/path/to/pmlog
.procmailrc:#VERBOSE=yes
.procmailrc:#LOGABSTRACT=all
.procmailrc:INCLUDERC=/etc/procmail-rc/filter-potentialproblem-rc
.procmailrc:INCLUDERC=/etc/procmail-rc/filter-potentialproblem2-rc
.procmailrc:INCLUDERC=/etc/procmail-rc/filter-spamassassin08-rc
.procmailrc:
.procmailrc::0Wc:
.procmailrc:| env python ./.pmscript.py
.procmailrc:
.procmailrc::0
.procmailrc:.mail/

.pmscript.py:import sys
.pmscript.py:import email
.pmscript.py:import httplib
.pmscript.py:import urllib
.pmscript.py:
.pmscript.py:# Contstants...
.pmscript.py:email_addresses = ['test@example.com', 'info@example.com']
.pmscript.py:key = "telegram_botkey"
.pmscript.py:userid = "telegram_userid"
.pmscript.py:headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
.pmscript.py:hostname = "api.telegram.org"
.pmscript.py:uri = "/bot" + key + "/sendMessage"
.pmscript.py:
.pmscript.py:# Read from stdin...
.pmscript.py:full_msg = sys.stdin.read()
.pmscript.py:msg = email.message_from_string(full_msg)
.pmscript.py:
.pmscript.py:# Get info from message...
.pmscript.py:mto = msg['to']
.pmscript.py:msubject = msg['subject']
.pmscript.py:mfrom = msg['from']
.pmscript.py:
.pmscript.py:# Check if e-mail in list...
.pmscript.py:for mailaddr in email_addresses:
.pmscript.py:  if mailaddr in mto:
.pmscript.py:    mrow = "[S6:NewMailAlert] \nFrom: " + mfrom + " \nSubject: " + msubject
.pmscript.py:    params = urllib.urlencode({'chat_id': userid, 'disable_web_page_preview': '1', 'text': mrow})
.pmscript.py:    connectie = httplib.HTTPSConnection(host=hostname, port=443)
.pmscript.py:    connectie.request("POST", uri, params, headers)
.pmscript.py:    resultaat = connectie.getresponse()
.pmscript.py:    if resultaat.status <> 200:
.pmscript.py:      print resultaat.status, resultaat.reason

Categories
PowerShell Scripting

PowerShell Validate XML Message

Quick (raw) guide to validate a XML message using Powershell.

# Declare schemas opbject…
$schemas = New-Object System.Xml.Schema.XmlSchemaSet

# Read schema from file…
$schemaItem = Get-Item File.xsd
$fileStream = $schemaItem.OpenRead()
$xsd = [Xml.Schema.XmlSchema]::Read($fileStream, $null)
$fileStream.Close()

# Add XSD to schemas & compile…
$schemas.Add($xsd)
$schemas.Compile()

# Read & validate XML…
[xml]$xml = Get-Content .\File.xml
$xml.Schemas = $schemas
$xml.Validate($null)

Categories
PowerShell Scripting

PowerShell Cleanup Script

Cleanup logfiles when file change date is older than 30 days…

<#
    Description    : Cleanup logfiles when file change date is older than 30 days...
    Keywords       : Files, Change, Cleanup

    Majorversion   : 1

    Author         : Tim van Kooten Niekerk
    Date           : 2011-11-10
#>

<#      CHANGELOG:
        2013-12-12 => Add Headers (Tim van Kooten Niekerk)

#>


# Global config settings...
[string]$sDirName = "D:\LogFiles\W3SVC705438624"
[string]$sFileNameMask = "*.log"
[string]$sGCIMask = $sDirName + "\" + $sFileNameMask
[string]$sEventLogMessage = "### Logfile cleanup script started ###`r`n"


# Start Main script...
if (Test-Path $sDirName) {
get-childitem $sGCIMask | 
	% { 
		if ($_.LastWriteTime -lt (Get-date).AddDays(-30)) {
		remove-item $_.fullname
		$sEventLogMessage = $sEventLogMessage + "File " + $_.fullname + " (" + $dFileModTime.DateTime + ") removed...`r`n"
		}
	}
}


# Cleanup...
[string]$sEventLogMessage = $sEventLogMessage + "### Logfile cleanup script finished ###"
write-eventLog -LogName "Windows PowerShell" -Source "PowerShell" -EventID 30001 -Message $sEventLogMessage -EntryType Information

Categories
PowerShell Scripting

PowerShell Admin Functions (Module)

Some usefull (and less usefull) admin functions…

function fnGetFQDN() {
  param (
	  [switch]$swIsEmail = $false
  )
  $oNETIP = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
  if ($swIsEmail -eq $true) {
    [string]$Return1 = "noreply@" + $oNETIP.HostName + "." + $oNETIP.DomainName
  } else {
    [string]$Return1 = $oNETIP.HostName + "." + $oNETIP.DomainName
  }  
  return $Return1
}
Categories
MySQL Server

MySQL Convert UTC Timestamp to Specific Timezone

MySQL example to convert UTC timestamp to specific timezone.

SELECT 
  SensorName, 
  MAX(CONVERT_TZ(LastUpdated, 'UTC', 'Europe/Amsterdam')) AS LastUpdated
FROM Sensors 
WHERE (TIME_TO_SEC(TIMEDIFF(NOW(), CONVERT_TZ(LastUpdated, 'UTC', 'Europe/Amsterdam'))) < 1800) 
GROUP BY SensorName;