Quantcast
Channel: Syed Jahanzaib – Personal Blog to Share Knowledge !
Viewing all 408 articles
Browse latest View live

Fighting with Spoofed Emails in IBM Lotus Domino using Symantec SMSDOM

$
0
0

s1

From the Diary / 12th July, 2016


We are using Symantec IBM lotus Domino as our mailing system for inbound/outbound emails & Symantec Mail Security as anti-spam mechanism. Managing heavily used production email server & fighting with the spam is a really tough job to do and requires continuous monitoring and most times requires additional work to do on regular basis.

From past few days, our email users were receiving lot of spoofed (faked advertisement / malware) emails pretending to be coming from there own email address and sometimes other legitimate users as well.. Subject was different every time , and source was dynamic too in the header. It was really annoying as user does not wants to block his email address in filters.

E-mail spoofing is the forgery of an e-mail header so that the message appears to have originated from someone or somewhere other than the actual source. Example you can receive email pretending to be coming from your own email address, strange and annoying as well too.

Adding SPF record is a good idea and every mail server mx record should have it. However I took another route which is a kind of workaround BUT at least it’s working amazingly good for me !.

Just to share my story. / z@ib

 

I made following rule in SMSDOM Content Filter Rules Section.


Description: BLOCK SPOOFED EMAILS RULE

Classification: Compliance Rule

This rule is for: Email Routing

Flow: Inbound

This rule is applied: Conditionally

Condition: Unless

Attributes: Sender/Author > myself@mycompany.com
[Exempt my few local servers email ids that are used to send backup/alerts emails to admin via using batch scripts]

Rule Expression: if Internet Domain Contains MYCOMAPNY.COM

Action: QUARANTINE the Document


Save the rule.

 

Images of rules,

1

2

3

4

 


End Results:

:) & now I can see many spoofed emails dropping in  the quarantine box and user’s inbox is clean and shiny.

spoofed_results_in_smsdom_quarantine

 


TIPS:

Test Spoofing

To test sending spoofed email, you can use following web site to do so ..

https://www.wormly.com/test_smtp_server

 

SPF RECORD: / zaib

To make SPF record on the DNS server, you can use following syntax

v=spf1 mx ip4:1.2.3.4 -all

Above record will allow all your MX records + IP 1.2.3.4 to send email from your domain, everything else is prohibited. But the mail servers or relays must support SPF protocol.

Or if you have two ISP links for primary and secondary mx, (two ip addresses), You can use following

v=spf1 mx ip4:1.2.3.4 ip4:5.6.7.8 -all

whereas 1.2.3.4 , 5.6.7.8 are the Public ip address of your email server.

OR something like

spf1

In above image, 1.2.3.4 is primary internet link IP for email server, and 5.6.7.8 is seconday backup internet link IP , so I added both in the record.


Regard’s

Syed Jahanzaib

 


Filed under: IBM Related

Mikrotik reboot alert with false detection checks

$
0
0

reboot

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,

# 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 Syed Jahanzaib
# https://aacable.wordpress.com
# Email : aacable at hotmail dot com
# Script Last Modified : 20th-JUL-2016 / 1100 Hours

# 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 / zaib
: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 / zaib
:delay 180s

# GMAIL Setup
:local gmailid "YOUR-GMAIL-ID@gmail.com"
:local GMAILPASS "YOUR-GMAIL-PAS"
:global gmailsmtp
:set gmailsmtp [:resolve "smtp.gmail.com"];
:local COMPANY "ZAIB"

# 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 "03333021909"
:local cell2 "0333xxxxxxx"

: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 aacableAThotmailDOTcom

:local mailsendto2
:set mailsendto2 secondadmin@gmailDOTcom
# 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 powered by SYED JAHANZAIB !"

# 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
# Syed Jahanzaib
# aacable [at] hotmail.com


Results~

Screenshot_2016-07-20-11-52-17

 

rebootmail

 


Regard’s
Syed Jahanzaib~


Filed under: Mikrotik Related

Lotus Notes / Copy – Duplicating prohibtited

$
0
0

pmail.jpg


In our company, we have IBM Lotus Domino Mail Server which i managed myself. Getting Lotus Domino support is quite a tough job, especially if you dont have any support SLA with the IBM, which generally costs heavy amount in $. Therefore I have to manage things on my own mostly using google and with some common sense lol.

Today we received an email from a valid client, and when we tried to copy or reply him with history, we receive following error.

w2.PNG

It also happens if user have selected following in mail delivery options.

w1.PNG

Without going in much details (which is already available in greater details on the internet), here is how I managed to sort it.


Requirements: Domino Admin Client.

Open user mail file via Domino Admin Client.
Goto Create / Agent,

As showed in the image below …

formula.PNG

 

Make sure you select FORMULA as shown above, and copy paste following code …

FIELD $KeepPrivate := @DeleteField;

Save it with any name like “remove keep private” and exit.


Lotus Notes Client:

Now open Lotus Notes Client , goto inbox and open the affected email,

Now goto Action / and you will see the newly created agent name. click on it.

As showed in the image below …

agent.png

it may take just a second or two most, and will remove the restriction:)

Enjoy !

Syed Jahanzaib

 


Filed under: IBM Related, Uncategorized

Data Center Temperature Monitoring via BASH Script

$
0
0

2016-07-30 06.21.20


Following is an bash script to monitor data center temperature via SNMP query from Emerson UPS. We can monitor the temperature via manageable ups builtin feature as well, but since i was required to do some extra functions which were not supported by the ups web panel, so i made following. although i removed few functions, but sharing it for general public as an example.

Following is an example code of SNMP query of temperature monitor from Emerson UPS ITA 10k UPS. [Sensor IRM-S02TH-001]

root@linux:/temp# snmpwalk -v2c -Oqv -c public  10.0.0.2  1.3.6.1.4.1.13400.2.62.2.1.2.0

2070

#!/bin/bash
# Function: DATA CENTER TEMPERATURE ALERT SCRIPT
# Scheduled Script to check data center temperature via SNMP after every 5 minutes.
# If it found high or above our defined limit, send sms or email Alerts, but donot repeat it untill next status change.
# Script Designed by Syed Jahanzaib
# aacable at hotmail dot com
# http://aacable . wordpress . com
#set -x

# Colors Config . . . [[ JZ . . . ]]
ESC_SEQ="\x1b["
COL_RESET=$ESC_SEQ"39;49;00m"
COL_RED=$ESC_SEQ"31;01m"
COL_GREEN=$ESC_SEQ"32;01m"
DATE=`date`

# COMPANY NAME
COMPANY="ZABBO"

# Hostname
HOSTNAME=`hostname`

# KANNEL SMS Gateway Info
KANNELURL="10.0.0.1:13013"
KANNELID="kannel"
KANNELPASS="kannelpass"
CELL1="03333021909"

# GMAIL DETAILS
GMAILID="YOURGMAILID@gmail.com"
GMAILPASS="YOURGMAIL-PASS"
ADMINMAIL1="ADMIN-MAIL@hotmail.com"
SENDMAILAPP="/temp/sendEmail-v1.56/sendEmail"
STATUS_HOLDER="/tmp/datacentertemperature.txt"

# If temporary status holder is not present , then create it,
# forumla is being applied to prevent repeated attempt of file creation / zaib
if [ ! -f $STATUS_HOLDER ]; then
echo -e "Creating Status Holder for first time usage"
touch $STATUS_HOLDER
fi

# SMS and email msg fromat for up n down
MSG_UP="$COMPANY Alert:

$LOWMSG $DATE
$FOOTER"

MSG_DOWN="$COMPANY Alert:

$HIGHMSG @ $DATE
$FOOTER"

# UPSIP , example i m using is emerson UPS
UPSIP="10.0.0.2"
UPSTEMPOID="1.3.6.1.4.1.13400.2.62.2.1.2.0"
UPSSNMPCOM=public"

# set Temperaturte limit
TEMPLIMIT="24"

# Check for temperature via SNMP query, make sure to chhange it accordingly
# currently i am using emerson UPS with snmp web card, as example
TEMPRATURE=`snmpwalk -v2c -Oqv -c $UPSSNMPCOM $UPSIP $UPSTEMPOID`
# divide temperature formula which comes like 2100 , so divide with /100 so we get 21 actuall, just an example
TEMPFINAL=$(($TEMPRATURE / 100))

HIGHMSG="$COMPANY Data Center Temperature is HIGH, that is $TEMPFINAL"
LOWMSG="$COMPANY Data Center Temperature is OK , that is $TEMPFINAL"
FOOTER="Powered by Syed Jahanzaib"
UPMSG="/tmp/upmsg.sms"
DOWNMSG="/tmp/downmsg.sms"
# Print Values
echo -e "Maximum Temperature Allowed = $COL_GREEN $TEMPLIMIT c $COL_RESET
Current Temperature = $COL_RED $TEMPFINAL c$COL_RESET"

# Matchign Formula starts here .. zaib
# IF temperature result is greater the our defined limit, then give alert
if [ "$TEMPFINAL" -gt "$TEMPLIMIT" ]; then
echo -e "$COL_RED $HIGHMSG $COL_RESET"
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 1 ]; then
echo -e "$COL_RED SMS/Email have already been sent $COL_RESET"
fi
fi

# IF temperature result is greater the our defined limit, then send sms and email, IF NOT ALREAY SENT
if [ "$TEMPFINAL" -gt "$TEMPLIMIT" ]; then
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 0 ]; then
echo -e "$COL_RED ALERT: $HIGHMSG $(date) / SENDING SMS/Email .... $COL_RESET"
echo "$MSG_DOWN" > $DOWNMSG

# Sending DOWN SMS via KANNEL
cat $DOWNMSG | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-
# Sending Email via sendEmail tool app using GMAIL
$SENDMAILAPP -u "$HIGHMSG @ $DATE" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$DOWNMSG -o message-content-type=text

echo "TEMP" > $STATUS_HOLDER
fi
else
echo -e "$COL_GREEN $LOWMSG ... $COL_RESET"
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 1 ]; then
echo -e "$COL_GREEN $COMPANY ALERT : $HEADING $LOWMSG $(date) / SENDING SMS/Email .... $COL_RESET"
echo "$MSG_UP" > $UPMSG
# Sending UP SMS via KANNEL
cat $UPMSG | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-
# Sending Email via sendEmail tool app using GMAIL
$SENDMAILAPP -u "$LOWMSG @ $DATE" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$UPMSG -o message-content-type=text
sed -i "/TEMP/d" "$STATUS_HOLDER"
fi
fi

# Script Ends Here
# Syed Jahanzaib / aacable @ hotmail . com
# http:// aacable . wordpress . com

alert


Result:

High Temperature Alert Email Sample:

mailhigh

 

Low Temperature Alert Email Sample:

lowtemp


SMS RESULT:

2016-07-30 06.21.20


Filed under: Linux Related

BASH script to monitor Remote Host Port with Ping

$
0
0

portmonitor

Following script was designed for an OP who wanted to monitor his FTP Media sharing server network connectivity along with application running status. Previously the OP was using simple ping script to monitor the FTP server connectivity status, but later realized that most of time the FTP server network connectivity remains OK, but the FTP application (in this particular case its HFS) get crashes after prolonged use. So it was must to monitor the application PORT along with the network connectivity as well.


Requirements:

  • Check remote host network connectivity by ping, if found not responding, do not check further to query port status, send alert ping connectivity down alert 1 time until next status change,
  • If Ping responds ok, then check the port status, if found not responding, then send alert 1 time until next status change.

Solution:

I made following script which checks for network connectivity by PING and then query port running status using ‘nc’. This is just an example. You can use your own techniques to acquire the same result. This is fully tested and working script. There are many other ways to do the same like using mikrotik netwatch tool which is way too simple, or use any NMS app like Nagios, or DUDE which have good GUI control so no need to do coding in the dark : )

Surely this contains too much junk or some unwanted sections, so you may want to trim it according to your taste and requirements.

Regard’s
Syed Jahanzaib

 

the Script!

  • mkdir /temp
  • cd /temp
  • touch portmon.sh
  • chmod +x portmon.sh
  • nano portmon.sh

and paste following, make sure to edit all info accordingly…


#!/bin/sh
#set -x
# Script to check HOST ping and specific port. / zaib
# If found Down, then send one time sms/email until the next status change.
# which prevent repeatedly sms for same status.
# useful to monitor any application.
# Querying port is done by using 'nc' tool. you can use your own customized methods to query about any thing.
# For SMS we used local KANNEL as sms gateway
# For Email, we have used SENDEMAIL to send email via GMAIL. its a very simple tool and you can use your gmail account.
# This script took around 6 hours to complete because of multiple checks for up n down with prevention of repeated sms/email..
# You are free to use ,modify or distribute it. just keep the header intact.
# Regard's / # Syed Jahanzaib
# Email: aacable at hotmail dot com / http : // aacable . wordpress . com
# 5th August, 2016 / Jummah-tul-mubarak, 1st - yakum zilqad, 1437 Hijri

HOST="$1"
PORT="$2"
APPNAME="MIKROTIK"
HOSTNAME="hostname"
TEMP="temp"

# Check if folder exists, if not create one and continue, if already exists, ignore and process further ...
if [ ! -d "/$TEMP" ]; then
echo
echo
echo "/$TEMP folder not found, Creating it so all ping results should be saved there . . ."
mkdir /$TEMP
fi

COMPANY="ZABBO (Pvt) Ltd."
DATE=`date`

# GMAIL DETAILS
GMAILID="YOURGMAILID@gmail.com"
GMAILPASS="GMAILPASS"
ADMINMAIL1="YOURMAIL@hotmail.com"
SENDMAIL="/temp/sendEmail-v1.56/sendEmail"

# SMS RELATED and KANNEL INFO
# KANNEL SMS Gateway Info
KANNELURL="127.0.0.1:13013"
KANNELID="kannel"
KANNELPASS="KANNEL-PASS"
CELL1="03333021909"

#########################################
# SMS/EMAIL Messages for PING UP / DOWN #
#########################################

MSG_DOWN_PING="ALERT: $DATE

$HOST not responding to ping request. Check connectivity.

$COMPANY"

MSG_UP_PING="INFO: $DATE

$HOST is reachable now. OK!

$COMPANY"
# Temporary file holder for storing sms/email PING related msgs which will be sent to admin.
MSGDOWNHOLDER_PING="/$TEMP/$HOST.down.msg"
MSGUPHOLDER_PING="/$TEMP/$HOST.up.msg"
touch $MSGDOWNHOLDER_PING
touch $MSGUPHOLDER_PING

echo "$MSG_DOWN_PING" > $MSGDOWNHOLDER_PING
echo "$MSG_UP_PING" > $MSGUPHOLDER_PING

#########################################
# SMS/EMAIL Messages for PORT UP / DOWN #
#########################################

# SMS/EMAIL Messages for PORT UP / DOWN
MSG_DOWN_PORT="ALERT: $DATE

$HOST not responding to $port port request. Check $APPNAME.

$COMPANY"

MSG_UP_PORT="INFO: $DATE

$HOST $PORT port is reachable/responding now. OK!

$COMPANY"

# Temporary file holder for storing sms/email PORT related msgs which will be sent to admin.
MSGDOWNHOLDER_PORT="/$TEMP/$HOST.port.down.msg"
MSGUPHOLDER_PORT="/$TEMP/$HOST.port.up.msg"
touch $MSGDOWNHOLDER_PORT
touch $MSGUPHOLDER_PORT
echo "$MSG_DOWN_PORT" > $MSGDOWNHOLDER_PORT
echo "$MSG_UP_PORT" > $MSGUPHOLDER_PORT

HOST_DOWN_ALERTONSCREEN="ALERT ..... HOST $HOST PING is DOWN @ $DATE ..."
HOST_UP_ALERTONSCREEN="INFO ..... HOST $HOST PING is OK @ $DATE ..."
HOST_PORT_DOWN_ALERTONSCREEN="ALERT .... System ping is responding OK, BUT PORT number $PORT on $HOST is down @ $DATE"
HOST_PORT_UP_ALERTONSCREEN="INFO .... $APPNAME server port nummber $PORT on $HOST is responding OK @ $DATE"

# How many PING attempts
PING_ATTEMPTS=1

# Temporary file holder for host ping status
HOST_PING_STATUS="/$TEMP/$HOST.ping"

# Temporary file holder for host port status
HOST_PORT_STATUS="/$TEMP/$HOST.port"
# Create temp file if not already present, usually for 1st time execution
touch $HOST_PING_STATUS
touch $HOST_PORT_STATUS

### START ACTION
#################################
# CHECK PING SECTION USING PING #
#################################

##################################
### CHECK PING STATUS - for DOWN #
##################################

# Check if HOST is accessibel or not, if not then EXIT immediately with error / zaib
if [[ $(ping -q -c $PING_ATTEMPTS $HOST) == @(*100% packet loss*) ]]; then
echo "$HOST_DOWN_ALERTONSCREEN"
if [ $(grep -c "$HOST" "$HOST_PING_STATUS") -eq 1 ]; then
echo "Host DOWN SMS have already been sent to $CELL1 ... "
fi
if [ $(grep -c "$HOST" "$HOST_PING_STATUS") -eq 0 ]; then
echo "SENDING HOST PING DOWN SMS/EMAIL ..."
echo "$HOST" > $HOST_PING_STATUS

# Sending PING DOWN ALERT via EMAIL
$SENDMAIL -u "$HOST_DOWN_ALERTONSCREEN" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$MSGDOWNHOLDER_PING -o message-content-type=text

# Sending PING DOWN ALERT via SMS
cat $MSGDOWNHOLDER_PING | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-

echo "SMS/EMAIL Sent DONE only 1 time until next status change ..."
fi
# If host is DOWN, then send sms/email alert & exit.
exit 1

################################
### CHECK PING STATUS - for UP #
################################

else
echo "$HOST_UP_ALERTONSCREEN"
if [ $(grep -c "$HOST" "$HOST_PING_STATUS") -eq 1 ]; then
echo "$HOST is responding OK now, and SMS/EMAIL Sent for UP sent DONE only 1 time until next status change ..."
sed -i "/$HOST/d" "$HOST_PING_STATUS"
# Sending PING UP ALERT via EMAIL
$SENDMAIL -u "$HOST_UP_ALERTONSCREEN" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$MSGUPHOLDER_PING -o message-content-type=text

# Sending PING DOWN ALERT via SMS
cat $MSGUPHOLDER_PING | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-

fi
fi

####################################
# CHECK PORT SECTION USING NC TOOL #
####################################

################################
### CHECK PORT STATUS - for UP #
################################

CHKPORT=`nc -z -w 1 $HOST $PORT; echo $?`
if [ $CHKPORT -eq 0 ]; then
echo -e "$HOST_PORT_UP_ALERTONSCREEN"

if [ $(grep -c "$HOST" "$HOST_PORT_STATUS") -eq 1 ]; then
echo "Sending UP SMS 1 time only"

# Sending PORT DOWN ALERT via EMAIL
$SENDMAIL -u "$HOST_PORT_UP_ALERTONSCREEN" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$MSGUPHOLDER_PORT -o message-content-type=text

# Sending PORT DOWN ALERT via SMS
cat $MSGUPHOLDER_PORT | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-

sed -i "/$HOST/d" "$HOST_PORT_STATUS"
fi
fi

##################################
### CHECK PORT STATUS - for DOWN #
##################################

if [ $CHKPORT -eq 1 ]; then
echo "$HOST_PORT_DOWN_ALERTONSCREEN"
if [ $(grep -c "$HOST" "$HOST_PORT_STATUS") -eq 1 ]; then
echo "Host PORT DOWN SMS have already been sent to $CELL1 ... "
fi
if [ $(grep -c "$HOST" "$HOST_PORT_STATUS") -eq 0 ]; then
echo "SENDING HOST PORT DOWN SMS ..."
echo "$HOST" > $HOST_PORT_STATUS
echo "SMS Sent FOR PORT DOWN DONE only 1 time until next status change ..."

# Sending PORT DOWN ALERT via EMAIL
$SENDMAIL -u "$HOST_PORT_DOWN_ALERTONSCREEN" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$MSGDOWNHOLDER_PORT -o message-content-type=text

# Sending PORT UP ALERT via SMS
cat $MSGDOWNHOLDER_PORT | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-

fi
fi
####################
# SCRIPT ENDS HERE #
# SYED JAHANZAIB #
####################


Usage:

change the IP and port number.

  • /temp/portmon.sh 192.168.20.1 1235

You can add entry in cron like this

# Check for Service remote host port status
*/5 * * * * /temp/portmon.sh 192.168.20.1 1235

RESULT:

If ping not respond …

1- ping

 

If port not respond …

2- port

 

Email Alerts:

3- mail alert

 

SMS Alerts:

 

port alert1 (1)

 

port alert1 (2)

 


 


Filed under: Linux Related

UPS Monitor BASH Script with Multi Matching ‘IF Statements’

$
0
0

Not for general public, Following script is just For my personal reference Purpose only!!!

 

script ups output

 

Emerson UPS Info via DUDE

kesc

 


Scenario:

In our datacenter , we have Emerson 10kva UPS x 2 in failover + load balancing mode. Yesterday we got some electric failure resulting in UPS shutdown after its batteries banks got drained. This UPS doesn’t have advance alerting system other than simple traditional alerts of light failure and restore via email. Therefore I made some customized scripts which are now monitoring the UPS status with more deeper digging.

Example: If the Electricity failure occurs (or low voltage under xxx value) AND the remaining backup time goes below our defined threshold, then it should send email + sms alert to the admin. So basically we will match at least two conditions.



#!/bin/bash
# Function: DATA CENTER UPS KE INPUT and VOLTAGE ALERT SCRIPT
# Scheduled Script to check data center KE INPUt + UPS Backup remain time via SNMP after every 1 mnt.
# If it found lower than our defined limit, send sms or email Alerts, but do not repeat it until next status change.
# Script Designed by Syed Jahanzaib
# aacable at hotmail dot com
# http://aacable . wordpress . com
# Created : 8th-Aug-2016  @ AGP
# set -x

# Colors Config . . . [[ JZ . . . ]]
ESC_SEQ="\x1b["
COL_RESET=$ESC_SEQ"39;49;00m"
COL_RED=$ESC_SEQ"31;01m"
COL_GREEN=$ESC_SEQ"32;01m"
DATE=`date`

# COMPANY NAME
COMPANY="ZABBO Pvt Ltd."
FOOTER="Powered by Syed Jahanzaib / AGP Pvt. Ltd."

# Hostname
HOSTNAME=`hostname`

# KANNEL SMS Gateway Info
KANNELURL="KANNEL-URL:13013"
KANNELID="kannel"
KANNELPASS="KANNEL-PASS"
CELL1="03333021909"

# GMAIL DETAILS
GMAILID="YOURMGAILID@gmail.com"
GMAILPASS="GMAILPASS"
ADMINMAIL1="aacable@hotmail.com"
SENDMAILAPP="/temp/sendEmail-v1.56/sendEmail"
STATUS_HOLDER="/tmp/upsmon.txt"

# UPS IP / SNMP RELATED INFO
UPSIP="$1"
SNMPSTR="public"
# OID For Emerson rack base UPS 10kvs with snmp web card
UPSINPUTOID="1.3.6.1.4.1.13400.2.16.2.2.1.0"
UPSTIMEOID="1.3.6.1.4.1.13400.2.16.2.5.2.0"

# Minimum Backup time threshold limit, below this action can be triggered if matched with second trigger
UPSTIMELIMIT="30"
# Minimum KE INput electric voltages limit, below this , both condition can be matched.
KELIMIT="190"

# Check if UPS IP is accessible or not, if not then EXIT immediately with error / zaib
if [[ $(ping -q -c 1 $UPSIP) == @(*100% packet loss*) ]]; then
echo "ALERT ..... UPS IP $UPSIP is DOWN ..."
exit 1
fi

# Check if SNMP query is accessible or not, if not then EXIT immediately
SNMPRESULT="/tmp/snmpoutput.txt"
touch $SNMPRESULT
snmpwalk -v1 -c agp $1 $UPSINPUTOID > $SNMPRESULT
if [ ! -s $SNMPRESULT ]; then
echo "ALERT ALERT: UPS IP $UPSIP - SNMP NOT Responding"
exit 1
fi

# If temporary status holder is not present , then create it,
# forumla is being applied to prevent repeated attempt of file creation / zaib
if [ ! -f $STATUS_HOLDER ]; then
echo -e "Creating Status Holder for first time usage"
touch $STATUS_HOLDER
fi

# Check for KE INPUT via SNMP query, make sure to chhange it accordingly
# currently i am using emerson UPS with snmp web card, as example
TIMEQ=`snmpwalk -v2c -Oqv -c $SNMPSTR $UPSIP $UPSTIMEOID`
KEINPUT=`snmpwalk -v2c -Oqv -c $SNMPSTR $UPSIP $UPSINPUTOID`
# divide time formula which comes like 2100 , so divide with /100 so we get 21 actually, just an example
KE=$(($KEINPUT / 100))

# Message template for up n down alerts
LOWMSG="$COMPANY Data Center UPS K.E Input have failed.
UPS IP = $UPSIP
Current K.E Input = $KE Volts
Remaining backup time = $TIMEQ mnts
Please check urgent"

OKMSG="$COMPANY Data Center UPS KESC Input have restored.
UPS IP = $UPSIP
Current K.E Input = $KE Volts
Remaining backup time = $TIMEQ mnts"

NORMSG="All Seems OK !"
UPMSG="/tmp/upmsg.sms"
DOWNMSG="/tmp/downmsg.sms"

# SMS and email msg fromat for up n down
MSG_UP="$COMPANY Alert:

$OKMSG @ $DATE
$FOOTER"

LOWMSG="$COMPANY Alert:

$LOWMSG @ $DATE
$FOOTER"

# Print Current fetched Values
echo -e "
UPS IP / PING response = $UPSIP / Ping Responding OK
UPS SNMP Response = OK / SNMP query Accessible
Minimum Time Threshold = $COL_GREEN $UPSTIMELIMIT mnts $COL_RESET
Minimum KE Input Threshold = $COL_GREEN $KELIMIT volts $COL_RESET
Current Backup Time = $COL_RED $TIMEQ mnts $COL_RESET
Current K.E Input = $COL_RED $KE volts $COL_RESET"

# Check if ke failure alert have already been sent, if yes then inform accordingly.
if [[ "$TIMEQ" -lt "$UPSTIMELIMIT" ]] && [[ "$KE" -lt "KELIMIT" ]]; then
echo -e "$COL_RED $LOWMSG $COL_RESET"
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 1 ]; then
echo -e "$COL_RED SMS/Email for DOWN have already been sent $COL_RESET"
fi
fi

# Matching Formula starts here .. zaib
# IF KE INPUT result is less than our defined limit and remaining time is low as well, ,
# then send sms and email, IF NOT ALREADY SENT
if [[ "$TIMEQ" -lt "$UPSTIMELIMIT" ]] && [[ "$KE" -lt "KELIMIT" ]]; then
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 0 ]; then
echo -e "$COL_RED ALERT: $LOWMS $(date) / SENDING SMS/Email .... $COL_RESET"
echo "$LOWMSG" > $DOWNMSG

# Sending DOWN SMS via KANNEL
cat $DOWNMSG | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-

# Sending Email via sendEmail tool app using GMAIL
$SENDMAILAPP -u "$LOWMSG @ $DATE" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$DOWNMSG -o message-content-type=text

echo "TEMP" > $STATUS_HOLDER
fi
else
echo -e "$COL_GREEN $NORMSG ... $COL_RESET"
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 1 ]; then
echo -e "$COL_GREEN $COMPANY ALERT : $OKMSG $(date) / SENDING OK SMS/Email .... $COL_RESET"
echo "$OKMSG" > $UPMSG

# Sending UP SMS via KANNEL
cat $UPMSG | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-

# Sending UP / OK Email via sendEmail tool app using GMAIL
$SENDMAILAPP -u "$OKMSG @ $DATE" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$UPMSG -o message-content-type=text
sed -i "/TEMP/d" "$STATUS_HOLDER"
fi
fi

# UPS Monitor Script Ends Here
# Syed Jahanzaib / aacable @ hotmail . com
# http:// aacable . wordpress . com


 

failure

Regard’s
Syed Jahanzaib

 


Filed under: Linux Related

Radius Manager Connection Tracking System for Mikrotik

$
0
0

trackme

As requested by few OP, following is a short technical reference guide on how you can enable TCP/UDP connections in Mikrotik to be stored in Radius manager connection tracking database so that you can view user base connection tracking report. In my personal view, it’s not much useful and at low end hardware it creates bottleneck, but if you have good resources with some fast storage (like SSD or RAID-10) it is better to set it up.

CTS is best described in the manual by dmasoftlab manual as mentioned below

Radius Manager has a special feature: the Connection Tracking System. It is available only in Radius Manager CTS version or higher. With the help of it the system can track and log all the TCP and UDP connections for all registered (online) users.

By default when You install the CTS enabled version of Radius Manager, it will use the default CTS database (CONNTRACK). It is strongly recommended to use a separate database host for the CONNTRACK database, due to the enormous amount of data stored daily. It can be even a 100-500 MegaBytes (and in my personal experience it can grow as much as 3-5 GB on busy network, ZAIB) per day. Fast disks (like SSD in RAID 10 mode,  zaib) are also recommended to be able to seek and store the data in real time. Radius Manager periodically stores the traffic data to CONNTRACK database (typically in
every 5–60 seconds).


Mikrotik (6.x) Configuration to enable Firewall Logging to remote server (RM)

If you have already configured the radius manager, then the conntrack database also get configured via the installation script. Next step is to enable the firewall logging in the mikrotik router so that mikrotik can send the categorized TCP/UDP data to radius manager conntrack database.

In mikrotik, open terminal and issue following commands …

In below example, we have following IP scheme.

PPPoE users ip pool = 172.16.0.1-172.16.0.255
Radius Manager IP  = 101.11.11.254

/ip firewall filter add chain=forward src-address=172.16.0.1-172.16.0.255 protocol=tcp connection-state=new action=log

/ip firewall filter add chain=forward src-address=172.16.0.1-172.16.0.255 protocol=udp connection-state=new action=log

/system logging action add name=rmctszaib remote=101.11.11.254 target=remote remote-port=4950

/system logging add topics=firewall action=rmctszaib

If you don’t see any errors , you are good to Go.


RADIUS MANAGER SECTION:

1. Radius Manager should be licensed with CTS level. Basic license wont gonna work ..

2. Make sure that  RMCONNTRACK service is UP and running.To make sure it’s running , get its process by

ps aux |grep rmconntrack

and you should get result something like following

root@radius:/# ps aux |grep rmconntrack
root xxxx 0.0 0.0 xxxxxx xxx ? Ssl 13:22 0:00 /usr/local/bin/rmconntrack

if you see its running, proceed further , otherwise you may start it manually by

rmconntrack –x

Now login to RM ACP  >  Reports  >  Connection Report

Here you can get report for all or individual user.

As showed in the image below …

c1

 

c2

 


Regard’s
Syed Jahanzaib

 

 

1


Filed under: Mikrotik Related, Radius Manager

Kannel SMS Gateway Delivery Reports in mySQL via sqlbox

$
0
0

kannel sqlbox sms report with DLR / modified tables and triggers/ zaib

Following is a reference guide on “Sending messages via kannel using SQLBOX / Triggers or PHP and storing the DLR in tables for tracking purposes” subject. This is not complete guide , just short notes that I was able to achieve.

There are few methods doing the same stuff. following is a mesh up configurations. You may find few parts useful or as reference purposes. All of these information was gathered from google, kannel mailing list, different blogs like (http://www.blogalex.com/archives/322) , 50-60 hours of eyes crushing efforts , mainly due to my slow pickup speed and efforts-in-the-wrong-direction approach.

My thoughts on this particular case are:
I have to admit that after spending so many years in this field, I am still unable to make a deep dive in this ocean. I am still doing basic surfing , nothing more then that. / z@iB


Requirements:

We are running SMS gateway using KANNEL and submit via URL http api. The OP required monthly report for the SMS sent to the users, delivery status of how many failed/delivered.

Solution:

We can make this happen by few methods. In this post, we have used SQLBOX in which we don’t require any external script like php which can update the status (although using external is more preferred and can perform many other functions as well ) . In this method we will simply insert our message in mysql SND table, and then the sqlbox and triggers will move this message to send_sms table, from where sqlbox will send the sms and move this entry to sendt_sms table, from there trigger will update the delivery status automatically (once dlr received).

Let’s Start

Components Used in this guide:

OS Used : Ubuntu 12.4 / 32bit
Mysql Version : mysql  Ver 14.14 Distrib 5.5.49, for debian-linux-gnu (i686) using readline 6.2
Kannel Version : Kannel bearerbox version `1.4.3′. Build `Nov 24 2011

I assume you have working kannel configuration and can send SMS successfully. For kannel installation and basics you can go through following guide.

https://aacable.wordpress.com/2012/11/26/howto-configure-your-local-http-gateway-using-kannel-on-ubuntu/

Once you have managed to configured the kannel in working state, follow below …


1- INSTALL SQLBOX

Install SQLBOX

sudo apt-get install kannel-sqlbox

2- Edit KANNEL.CONF

Now edit Kannel Config file

nano /etc/kannel/kannel.conf

and paste following data. Make sure to change the required fields


# Syed Jahanzaib
# https://aacable.wordpress.com
# aacable [at] hotmail.com
# KANNEL CONFIGURATION FILE WITH SQLBOX/MYSQL SUPPORT
# LAST MODIFIED AT 06-AUG-2016

group = core
admin-port = 13000
smsbox-port = 13001
admin-password = zaibadmin
status-password = zaibstatus
log-file = "/var/log/kannel/bearerbox.log"
log-level = 0
box-deny-ip = "*.*.*.*"
box-allow-ip = "127.0.0.1"
#Send sms only once. No retry
sms-resend-retry = 1
# DLR storage set to mysql
dlr-storage = mysql

group = smsc
smsc = at
smsc-id = zaibgsmid
modemtype = teltonika # in this example i have used teltonika serial modem g/10 / zaib
device = /dev/ttyS0
#transceiver-mode = true
# Change above port to match your port, it could ACM1 or USB0, if you dont set it correctly, it will not work and you will get error in logs that unable to connect to device)
speed = 115200 # make sure to change the speed or kannel wont be able to connect with your modem
my-number = 0333302100000 # can be any
# Following SMSC number is for zong
# sms-center = 00923040000011
sim-buffering = true
log-level = 0

# for teltonkia only, if you are using other brand you may need to get there INIT strings, use WDIALCONFIG for quering string
# But what I have seen that this string worked for dlink and huawei modem as well.
group = modems
id = teltonika
name = "Teltonika E12"
detect-string = "Undefined"
enable-mms = true
init-string = "ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0;+CNMI=1,3,2,2,1;+CMEE=1;+CPMS="SM","SM","SM";+CMGF=0"
# or i used this for dlink as well.
#init-string = "ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0"
speed = 115200
message-storage = "SM"

group = smsbox
bearerbox-host = localhost
sendsms-port = 13013
global-sender = 13013
sendsms-chars = "0123456789 +-"
log-file = "/var/log/kannel/smsbox.log"
log-level = 0
access-log = "/var/log/kannel/access.log"

group = sendsms-user
# MAKE VERY SURE that you change the PASSWORD for security reasons
username = kannel
password = kannelpass
concatenation = 1
max-messages = 10

group = sms-service
keyword = default
get-url = "http://localhost/playsms/index.php?app=call&cat=gateway&plugin=kannel&access=geturl&t=%t&q=%q&a=%a&Q=%Q" # this is for playSMS
accept-x-kannel-headers = true
max-messages = 0
concatenation = true
catch-all = true
text = "Galaxy ROBOT by Zaib"
omit-empty = true

################# MYSQL CONFIGURATION #############
# SQL BOX CONFIG
# Database connection examples. Please uncomment as needed
# Example MYSQL Connection

group = mysql-connection
id = sqlbox-db
host = localhost
username = root
password = rootpassword
database = kannel_db

# DLR SETUP
#mysql connection
group = mysql-connection
id = mydlr
host = localhost
username = root
password = rootpassword
database = kannel_db

group = dlr-db
id = mydlr
table = dlr
field-smsc=smsc
field-timestamp=ts
field-destination=destination
field-source=source
field-service=service
field-url=url
field-mask=mask
field-status=status
field-boxc-id=boxc

Save & exit.

Now edit SQLBOX Config file

nano /etc/kannel/sqlbox.conf

and paste the following


#SQL BOX CONFIG
group = sqlbox
id = sqlbox-db
smsbox-id = sqlbox
global-sender = "03333021909" # change this to SIM number
bearerbox-host = localhost
bearerbox-port = 13001
sql-log-table = sent_sms
sql-insert-table = send_sms
log-level = 0
log-file = "/var/log/kannel/kannel-sqlbox.log"

group = mysql-connection
id = sqlbox-db
host = localhost
username = root
password = rootpassword # change this for sure
database = kannel_db

Save & exit.


3- Creating Database for SQLBOX/SND/DLR and import

Create database by following command

mysql -uroot -pSQLPASS -e "create database kannel_db;"

Now create a file which will contain all the tables that are required for the kannel_db database. This is LAB testing version, but 100% working.

mkdir /temp
touch /temp/kannel_db_tables.sql
nano /temp/kannel_db_tables.sql

and paste the following data


-- MySQL dump 10.13 Distrib 5.5.49, for debian-linux-gnu (i686)
--
-- Host: localhost Database: kannel_db
-- ------------------------------------------------------
-- Server version 5.5.49-0ubuntu0.12.04.1

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `dlr`
--

DROP TABLE IF EXISTS `dlr`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `dlr` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`smsc` varchar(40) DEFAULT NULL,
`ts` varchar(40) DEFAULT NULL,
`destination` varchar(40) DEFAULT NULL,
`source` varchar(40) DEFAULT NULL,
`service` varchar(40) DEFAULT NULL,
`url` varchar(255) DEFAULT NULL,
`mask` int(10) DEFAULT NULL,
`status` int(10) DEFAULT NULL,
`boxc` varchar(40) DEFAULT NULL,
`time` bigint(20) NOT NULL,
`smsc_id` varchar(40) NOT NULL,
`sms_id` bigint(20) NOT NULL,
`msgdata` text NOT NULL,
`number` text NOT NULL,
`message` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=443 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `dlr`
--

LOCK TABLES `dlr` WRITE;
/*!40000 ALTER TABLE `dlr` DISABLE KEYS */;
/*!40000 ALTER TABLE `dlr` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `dlr2`
--

DROP TABLE IF EXISTS `dlr2`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `dlr2` (
`id_sms` bigint(20) NOT NULL AUTO_INCREMENT,
`smsc` varchar(40) DEFAULT NULL,
`msgid` text NOT NULL,
`destination` varchar(40) DEFAULT NULL,
`sendertype` text,
`url` varchar(255) DEFAULT NULL,
`mask` int(10) DEFAULT NULL,
`status` text,
`boxc` varchar(40) DEFAULT NULL,
`sms_insert_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`sms_delivery_date` timestamp NULL DEFAULT NULL,
`sms_message` text NOT NULL,
`status2` text NOT NULL,
PRIMARY KEY (`id_sms`)
) ENGINE=InnoDB AUTO_INCREMENT=293 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `dlr2`
--

LOCK TABLES `dlr2` WRITE;
/*!40000 ALTER TABLE `dlr2` DISABLE KEYS */;
/*!40000 ALTER TABLE `dlr2` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `send_sms`
--

DROP TABLE IF EXISTS `send_sms`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `send_sms` (
`sql_id` bigint(20) NOT NULL AUTO_INCREMENT,
`momt` enum('MO','MT') DEFAULT NULL,
`sender` varchar(20) DEFAULT NULL,
`receiver` varchar(20) DEFAULT NULL,
`udhdata` blob,
`msgdata` text CHARACTER SET utf8,
`time` bigint(20) DEFAULT NULL,
`smsc_id` varchar(255) DEFAULT NULL,
`service` varchar(255) DEFAULT NULL,
`account` varchar(255) DEFAULT NULL,
`id` bigint(20) DEFAULT NULL,
`sms_type` bigint(20) DEFAULT NULL,
`mclass` bigint(20) DEFAULT NULL,
`mwi` bigint(20) DEFAULT NULL,
`coding` bigint(20) DEFAULT NULL,
`compress` bigint(20) DEFAULT NULL,
`validity` bigint(20) DEFAULT NULL,
`deferred` bigint(20) DEFAULT NULL,
`dlr_mask` bigint(20) DEFAULT NULL,
`dlr_url` varchar(255) DEFAULT NULL,
`pid` bigint(20) DEFAULT NULL,
`alt_dcs` bigint(20) DEFAULT NULL,
`rpi` bigint(20) DEFAULT NULL,
`charset` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`boxc_id` varchar(255) DEFAULT NULL,
`binfo` varchar(255) DEFAULT NULL,
`meta_data` text,
`status` varchar(10) DEFAULT NULL,
PRIMARY KEY (`sql_id`)
) ENGINE=MyISAM AUTO_INCREMENT=782 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `send_sms`
--

LOCK TABLES `send_sms` WRITE;
/*!40000 ALTER TABLE `send_sms` DISABLE KEYS */;
/*!40000 ALTER TABLE `send_sms` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `sent_sms`
--

DROP TABLE IF EXISTS `sent_sms`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `sent_sms` (
`sql_id` bigint(20) NOT NULL AUTO_INCREMENT,
`datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`momt` enum('MO','MT','DLR') DEFAULT NULL,
`sender` varchar(20) DEFAULT NULL,
`receiver` varchar(20) DEFAULT NULL,
`udhdata` blob,
`msgdata` text,
`smsc_id` varchar(255) DEFAULT NULL,
`service` varchar(255) DEFAULT NULL,
`account` varchar(255) DEFAULT NULL,
`id` bigint(20) DEFAULT NULL,
`sms_type` bigint(20) DEFAULT NULL,
`mclass` bigint(20) DEFAULT NULL,
`mwi` bigint(20) DEFAULT NULL,
`coding` bigint(20) DEFAULT NULL,
`compress` bigint(20) DEFAULT NULL,
`validity` bigint(20) DEFAULT NULL,
`deferred` bigint(20) DEFAULT NULL,
`dlr_mask` bigint(20) DEFAULT NULL,
`dlr_url` varchar(255) DEFAULT NULL,
`pid` bigint(20) DEFAULT NULL,
`alt_dcs` bigint(20) DEFAULT NULL,
`rpi` bigint(20) DEFAULT NULL,
`charset` varchar(255) DEFAULT NULL,
`boxc_id` varchar(255) DEFAULT NULL,
`binfo` varchar(255) DEFAULT NULL,
`status` text,
`time` bigint(20) DEFAULT NULL,
PRIMARY KEY (`sql_id`),
KEY `ix_sent_sms_dlr_url` (`dlr_url`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `sent_sms`
--

LOCK TABLES `sent_sms` WRITE;
/*!40000 ALTER TABLE `sent_sms` DISABLE KEYS */;
/*!40000 ALTER TABLE `sent_sms` ENABLE KEYS */;
UNLOCK TABLES;
/*!50003 SET @saved_cs_client = @@character_set_client */ ;
/*!50003 SET @saved_cs_results = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode = @@sql_mode */ ;
/*!50003 SET sql_mode = '' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `after_sent_sms_insert` AFTER INSERT ON `sent_sms`
FOR EACH ROW BEGIN
UPDATE snd SET zaibstatus = 'Submitted to sqlbox' WHERE snd_id = NEW.dlr_url;

IF NEW.momt = 'DLR' THEN
CASE NEW.dlr_mask
WHEN 1 THEN UPDATE snd SET snd_success = FROM_UNIXTIME(NEW.time) , zaibstatus = 'SUCCESS' WHERE snd_id = NEW.dlr_url;
WHEN 2 THEN UPDATE snd SET snd_failure = FROM_UNIXTIME(NEW.time) , zaibstatus = 'FAILED wrong no' WHERE snd_id = NEW.dlr_url;
WHEN 4 THEN UPDATE snd SET snd_buffered = FROM_UNIXTIME(NEW.time) WHERE snd_id = NEW.dlr_url;
WHEN 8 THEN UPDATE snd SET snd_submitted = FROM_UNIXTIME(NEW.time) , zaibstatus = 'Pending' WHERE snd_id = NEW.dlr_url;
WHEN 16 THEN UPDATE snd SET snd_rejected = FROM_UNIXTIME(NEW.time) WHERE snd_id = NEW.dlr_url;
WHEN 32 THEN UPDATE snd SET snd_intermediate = FROM_UNIXTIME(NEW.time) WHERE snd_id = NEW.dlr_url;
ELSE UPDATE snd SET snd_last = NEW.dlr_mask WHERE snd_id = NEW.dlr_url;
END CASE;
END IF;
END */;;
DELIMITER ;
/*!50003 SET sql_mode = @saved_sql_mode */ ;
/*!50003 SET character_set_client = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection = @saved_col_connection */ ;

--
-- Table structure for table `snd`
--

DROP TABLE IF EXISTS `snd`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `snd` (
`snd_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'Every message has a unique id',
`snd_sender` varchar(255) DEFAULT NULL COMMENT 'The number that the message is sent from',
`snd_to` varchar(255) DEFAULT NULL COMMENT 'The number that the message is sent to',
`snd_txt` longtext COMMENT 'The body text of the message',
`snd_smsc` varchar(255) DEFAULT NULL COMMENT 'The number that the message is route to',
`snd_sentat` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'The time that the message is inserted',
`snd_success` datetime DEFAULT NULL COMMENT 'The time that the message was delivered to the hand set',
`snd_failure` datetime DEFAULT NULL COMMENT 'The time that the message failed',
`snd_submitted` datetime DEFAULT NULL COMMENT 'The time that the message is submitted to the smsc',
`snd_buffered` datetime DEFAULT NULL COMMENT 'The time that the message is buffered at the smsc',
`snd_rejected` datetime DEFAULT NULL COMMENT 'The time that the message was rejected',
`snd_intermediate` datetime DEFAULT NULL COMMENT 'Time of intermediate status',
`snd_last` int(11) NOT NULL DEFAULT '0',
`zaibstatus` text,
PRIMARY KEY (`snd_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='This table holds the messages that are sent, including DLR values.';
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `snd`
--

LOCK TABLES `snd` WRITE;
/*!40000 ALTER TABLE `snd` DISABLE KEYS */;
/*!40000 ALTER TABLE `snd` ENABLE KEYS */;
UNLOCK TABLES;
/*!50003 SET @saved_cs_client = @@character_set_client */ ;
/*!50003 SET @saved_cs_results = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode = @@sql_mode */ ;
/*!50003 SET sql_mode = '' */ ;
DELIMITER ;;
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER `after_snd_insert` AFTER INSERT ON `snd`
FOR EACH ROW BEGIN
INSERT INTO send_sms (momt, sender, receiver, msgdata, smsc_id, sms_type, dlr_mask, dlr_url, boxc_id, coding)
VALUES ('MT', NEW.snd_sender, NEW.snd_to, NEW.snd_txt, NEW.snd_smsc, 2, 31, NEW.snd_id, 'sqlbox', '2');
END */;;
DELIMITER ;
/*!50003 SET sql_mode = @saved_sql_mode */ ;
/*!50003 SET character_set_client = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection = @saved_col_connection */ ;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2016-09-06 10:32:14

Save & exit.

Now import these tables into mysql kannel_db database. use below command

mysql -uroot -pSQLPASS kannel_db < kannel_db_tables.sql

Login to mysql and see if you can see the database kannel_db and its tables.

mysql -uroot -pSQLPASS -e "use kannel_db; show tables from kannel_db;"

# and if all good, you will see tables as showed below ..
+---------------------+
| Tables_in_kannel_db |
+---------------------+
| dlr |
| dlr2 |
| send_sms |
| sent_sms |
| snd |
+---------------------+


5- Restarting the Services

stop the kannel and start the kannel bearerbox, smsbox and sqlbox in 3 separate terminals , to see for any errors.

service kannel stop

# wait few seconds

Now start the process , each process in separate terminal

bearerbox -v 0 /etc/kannel/kannel.conf

smsbox -v 0 /etc/kannel/kannel.conf

sqlbox -v 0 /etc/kannel/kannel.conf

If you see no errors you are all good to go … verify it by

 


root@ubuntu:~# ps aux |grep kannel
kannel 20324 0.0 0.0 2248 308 ? Ss 08:39 0:00 /usr/sbin/run_kannel_box --pidfile /var/run/kannel/kannel_bearerbox.pid --no-extra-args /usr/sbin/bearerbox -v 4 -- /etc/kannel/kannel.conf
kannel 20326 1.8 0.2 127896 10360 ? Sl 08:39 2:32 /usr/sbin/bearerbox -v 4 -- /etc/kannel/kannel.conf
kannel 20337 0.0 0.0 2248 356 ? Ss 08:39 0:00 /usr/sbin/run_kannel_box --pidfile /var/run/kannel/kannel_wapbox.pid --no-extra-args /usr/sbin/wapbox -v 4 -- /etc/kannel/kannel.conf
kannel 20341 0.0 0.0 2248 304 ? Ss 08:39 0:00 /usr/sbin/run_kannel_box --pidfile /var/run/kannel/kannel_smsbox.pid --no-extra-args /usr/sbin/smsbox -v 4 -- /etc/kannel/kannel.conf
kannel 20344 0.0 0.1 71236 4656 ? Sl 08:39 0:00 /usr/sbin/smsbox -v 4 -- /etc/kannel/kannel.conf

root@ubuntu:~# ps aux |grep sqlbox
root 20421 0.0 0.1 35096 4356 ? Ssl 08:39 0:01 sqlbox -d /etc/kannel/sqlbox.conf

Now try to insert data directly into snd table by using following command


mysql -uroot -pSQLPASS -e "use kannel_db; INSERT INTO snd (snd_sender,snd_to,snd_txt,snd_smsc) VALUES ('12345','03333021909', 'First Message sent from Kannel / SQLBOX and update DLR as well by zaib','zaibgsmid');"

in bearerbox logs, you will see something like following (Make sure your operator supports DLR)


2016-09-06 11:06:07 [8571] [10] DEBUG: boxc_receiver: sms received
2016-09-06 11:06:07 [8571] [10] DEBUG: send_msg: sending msg to boxc: <sqlbox>
2016-09-06 11:06:09 [8571] [6] DEBUG: AT2[zaibgsmid]: TP-Validity-Period: 24.0 hours
2016-09-06 11:06:09 [8571] [6] DEBUG: AT2[zaibgsmid]: --> AT+CMGS=76^M
2016-09-06 11:06:09 [8571] [6] DEBUG: AT2[zaibgsmid]: <-- AT+CMGS=76
2016-09-06 11:06:09 [8571] [6] DEBUG: AT2[zaibgsmid]: <-- >
2016-09-06 11:06:09 [8571] [6] DEBUG: AT2[zaibgsmid]: send command status: 1
2016-09-06 11:06:09 [8571] [6] DEBUG: AT2[zaibgsmid]: --> 0031000B813033031209F90000A746C6B47C4E0735CBF379F85C06CDCB6E3AC82C7FB741CBB0DB5D66835EA06994297C6241613719548793C3F43288C89482C273D0BDCC6683C479903E9C1603
2016-09-06 11:06:09 [8571] [6] DEBUG: AT2[zaibgsmid]: --> ^Z
2016-09-06 11:06:12 [8571] [6] DEBUG: AT2[zaibgsmid]: <-- > 0031000B813033031209F90000A746C6B47C4E0735CBF379F85C06CDCB6E3AC82C7FB741CBB0DB5D66835EA06994297C6241613719548793C3F43288C89482C273D0BDCC6683C479903E9C1603
2016-09-06 11:06:12 [8571] [6] DEBUG: AT2[zaibgsmid]: <-- +CMGS: 161
2016-09-06 11:06:12 [8571] [6] DEBUG: AT2[zaibgsmid]: <-- OK
2016-09-06 11:06:12 [8571] [6] DEBUG: AT2[zaibgsmid]: send command status: 0
2016-09-06 11:06:12 [8571] [6] DEBUG: DLR[mysql]: Adding DLR smsc=zaibgsmid, ts=161, src=0333302100000 # can be any, dst=03333021909, mask=31, boxc=sqlbox
2016-09-06 11:06:12 [8571] [6] DEBUG: sql: INSERT INTO dlr (smsc, ts, source, destination, service, url, mask, boxc, status) VALUES ('zaibgsmid', '161', '0333302100000 # can be any', '03333021909', '', '6', '31', 'sqlbox', '0');
2016-09-06 11:06:12 [8571] [6] DEBUG: SMSC[zaibgsmid]: creating DLR message
2016-09-06 11:06:12 [8571] [6] DEBUG: SMSC[zaibgsmid]: DLR = 6
2016-09-06 11:06:12 [8571] [11] DEBUG: send_msg: sending msg to boxc: <sqlbox>
2016-09-06 11:06:12 [8571] [11] DEBUG: boxc_sender: sent message to <127.0.0.1>
2016-09-06 11:06:15 [8571] [6] DEBUG: AT2[zaibgsmid]: <-- +CDSI: "MT",301
2016-09-06 11:06:15 [8571] [6] DEBUG: AT2[zaibgsmid]: +CMTI incoming SMS indication: +CDSI: "MT",301
2016-09-06 11:06:16 [8571] [6] DEBUG: AT2[zaibgsmid]: --> AT+CPMS="MT"^M
2016-09-06 11:06:16 [8571] [6] DEBUG: AT2[zaibgsmid]: <-- AT+CPMS="MT"
2016-09-06 11:06:16 [8571] [6] DEBUG: AT2[zaibgsmid]: <-- +CPMS: 9,350,1,50,1,50
2016-09-06 11:06:16 [8571] [6] DEBUG: AT2[zaibgsmid]: <-- OK
2016-09-06 11:06:16 [8571] [6] DEBUG: AT2[zaibgsmid]: --> AT+CMGR=301^M
2016-09-06 11:06:16 [8571] [6] DEBUG: AT2[zaibgsmid]: <-- AT+CMGR=301
2016-09-06 11:06:16 [8571] [6] DEBUG: AT2[zaibgsmid]: <-- +CMGR: 0,,25
2016-09-06 11:06:16 [8571] [6] DEBUG: AT2[zaibgsmid]: <-- 079129330320111006A10B813033031209F9619060116021026190601160310200
2016-09-06 11:06:16 [8571] [6] DEBUG: AT2[zaibgsmid]: received message from SMSC: +923330021101
2016-09-06 11:06:16 [8571] [6] DEBUG: AT2[zaibgsmid]: got STATUS-REPORT for message <161>:
2016-09-06 11:06:16 [8571] [6] DEBUG: AT2[zaibgsmid]: Numeric receiver <03333021909>
2016-09-06 11:06:16 [8571] [6] DEBUG: DLR[mysql]: Looking for DLR smsc=zaibgsmid, ts=161, dst=03333021909, type=1
2016-09-06 11:06:16 [8571] [6] DEBUG: sql: SELECT mask, service, url, source, destination, boxc FROM dlr WHERE smsc='zaibgsmid' AND ts='161';
2016-09-06 11:06:16 [8571] [6] DEBUG: Found entry, row[0]=31, row[1]=, row[2]=6, row[3]=0333302100000 # can be any, row[4]=03333021909 row[5]=sqlbox
2016-09-06 11:06:16 [8571] [6] DEBUG: DLR[mysql]: created DLR message for URL <6>
2016-09-06 11:06:16 [8571] [6] DEBUG: removing DLR from database
2016-09-06 11:06:16 [8571] [6] DEBUG: sql: DELETE FROM dlr WHERE smsc='zaibgsmid' AND ts='161' LIMIT 1;
2016-09-06 11:06:16 [8571] [11] DEBUG: send_msg: sending msg to boxc: <sqlbox>
2016-09-06 11:06:16 [8571] [11] DEBUG: boxc_sender: sent message to <127.0.0.1>
2016-09-06 11:06:16 [8571] [6] DEBUG: AT2[zaibgsmid]: <-- OK
2016-09-06 11:06:16 [8571] [6] DEBUG: AT2[zaibgsmid]: --> AT+CMGD=301^M
2016-09-06 11:06:16 [8571] [6] DEBUG: AT2[zaibgsmid]: <-- AT+CMGD=301

Now query the snd table, and you will the SUCCESS status (success status will appear only when DLR will be received from the mobile operator, which means if the receiver mobile is off, you will not receive DLR, once the message is delivered to the phone , then DLR will receive and status message will be udpated)


root@ubuntu:~# mysql -uroot -pSQLPASS -e "use kannel_db; select * from snd;"
+--------+------------+-------------+------------------------------------------------------------------------+-----------+---------------------+---------------------+-------------+---------------+--------------+--------------+------------------+----------+------------+
| snd_id | snd_sender | snd_to | snd_txt | snd_smsc | snd_sentat | snd_success | snd_failure | snd_submitted | snd_buffered | snd_rejected | snd_intermediate | snd_last | zaibstatus |
+--------+------------+-------------+------------------------------------------------------------------------+-----------+---------------------+---------------------+-------------+---------------+--------------+--------------+------------------+----------+------------+
| 6 | 12345 | 03333021909 | First Message sent from Kannel / SQLBOX and update DLR as well by zaib | zaibgsmid | 2016-09-06 11:06:07 | 2016-09-06 11:06:16 | NULL | NULL | NULL | NULL | NULL | 0 | SUCCESS |
+--------+------------+-------------+------------------------------------------------------------------------+-----------+---------------------+---------------------+-------------+---------------+--------------+--------------+------------------+----------+------------+

Final Success Screenshots

#send_sms table

final-success-1

 

#snd table

final_scucess

Now as all messages can be stored here in this snd_table along with their status , you can create a nice front end to fetch your required results.


Special thanks to

1- Mr. Tapan Kumar Thapa for sqlbox remote assistance for the triggers as I was not able to create the triggers due to mySQL syntax errors. He can be reached at “https://in.linkedin.com/in/tapan-thapa-02b68b5

2- Mr. Julien , who provided logics and few directions which helped me in dlr.php approach.


Regard’s
Syed Jahanzaib


Filed under: Linux Related

Sample PHP page to insert data into kannel/sqlbox

$
0
0

centralized-sms-ystem


Other kannel references:

Before Proceeding further, read the following links for Kannel Installation and configure SLQBOX.

https://aacable.wordpress.com/2012/11/26/howto-configure-your-local-http-gateway-using-kannel-on-ubuntu/

https://aacable.wordpress.com/2016/09/06/kannel-sms-gateway-delivery-reports-in-mysql-via-sqlbox/


Following is a sample PHP file which can be called by BASH/CURL or any remote system to send SMS via inserting the message into SND database which will send SMS to destination with the message. So it will act as a centralized SMS submission process that will keep record of every outgoing Sent SMS along with any detail that we requires, example sender (the requesting party), recipient, and the message (OR etc etc)

This is just for reference purposes of DOABLE things, and by no means it should be deployed in production environment. These are just short references, and must be modified according to the safety standards.

 


Background:

Currently we are using KANNEL as SMS gateway to send messages to users,admins about various events and information. This SMS Gateway is used by the Linux , Mikrotik Router, and windows base systems. But the problem is that using standard kannel HTTP submission method, we cannot track any message and its status. Its like blind system. And recently it was asked by few OP to track the Sent sms Numbers, status for a particular given time or over all month report.

 

Standard Examples of sending SMS via kannel HTTP submission:

Linux Example:

curl "$KURL/cgi-bin/sendsms?username=$KID&password=$KPASS&to=$mobile" -G --data-urlencode text@/tmp/$USR.sms
s

Windows Example:

C:\wget\wget.exe  "http://192.168.0.1:13013/cgi-bin/sendsms?username=kannel&password=KANNELPASS&to=03333021909&text=INFO: [TimeAndDate] UPS is working on KESC now"

Mikrotik Example:

/tool fetch url="$KURL\?username=$KID&password=$KPASS&to=$cell1+$cell2&text=$MSGUPSMS"

Requirements:

I wanted to have a simple PHP page that can be called by any system with only three fields

1- Sender (Example server1 or ExpiryDaemon)
2- To (mobile number of the recipient)
3- Message (which is usually large with multiple lines)

One Example for Linux base system as following:

The scheduled bash script runs on Linux base billing system which submits various messages to users like account expiry, password change info, etc via submitting messages with destination numbers using CURL.


Solution:

For this purpose we made simple PHP page that can be called from any remote system and will act using the provided url parameters. thanks to mr tapan.kumar for providing sample php page for this purpose.

Example of CURL/BASH script which fetch data from mysql table for  logged in alert for manager/admin into panel.

KANNELURL="127.0.0.1"
SENDER="Billing-adminlogin"
MOBILE="1234567890"
MSG="Dear $MANAGER ,
You have successfully logged-in to billing admin panel.
ID = $ID
DATE = $DATE
Thank You"

# Curl example that will call test.php with 3 parameters ...
curl -G "http://$KANNELURL/test.php?sender=php&to=$MOBILE" --data-urlencode "message=$MSG"

 

TEST.PHP SAMPLE


<?php
$host = 'localhost';
$user = 'root';
$password = 'SQLPASS';
$database = 'kannel_db';
$port = 3306;
$conn = mysql_connect($host, $user, $password);
mysql_select_db($database, $conn);
if (!$conn) {
die("Unable to connect with MySQL database.\n");
}
$sender = trim($_GET['sender']);
$to = trim($_GET['to']);
$message = trim($_GET['message']);
// I have already configured the SQLBOX so any entry made into SND DB, it will automatically move
// data into send_sms and will update the SND status as well on receiving DLR
$sqlQuery = "insert into snd (snd_sender,snd_to,snd_txt) values('$sender','$to','$message')";
mysql_query($sqlQuery);
echo 'OK';
?>

When the curl will call test.php , it will simply enter the data into SND DB.

Example:

snd_example

and from mikrotik it would be like

mikrotik-send-sms

 

This way we can have a centralized SMS submission system , which will keep track of every SENT sms with the details we require according the local need. It can be modified as per the requirements. These are just my findings from Google and tips with some working sample code from few resources.

 

Regard’s
Syed Jahanzaib


Filed under: Linux Related

Howto add Simple VPN (PPTP) Server in Ubuntu

$
0
0

VPN (PPTP) Server in Ubuntu


This post contains short notes on HOW-TO add simple pptp based VPN server in Ubuntu. It it useful in many scenarios example if you have Linux server in the office, and want to connect to it from anywhere in the world. OR you can use it in a situation where you want your billing server to act like a centralized billing system for all the remote locations NASES.

Ideally you should have static Public IP on this vpn server, but you can bypass this requirement by adding an DDNS name as well as a workaround like changeip.com. I remember that I did similar configuration at some philippine network and one at cloud.

 

Components Used:

OS = Ubuntu 12.4 on Cloud with static Public IP address
Clients = Windows 7 / Mikrotik Routerboards

Let’s Start…


Install PPTPD Package

Install PPTPD package in ubuntu by issuing following command

sudo apt-get -y install pptpd

Now Issue following to add vpn server options like local/remote ip addresses. I used echo command to directly insert text/data into the config files rather than editing by nano/vim. use whatever is easier for you.

# This will be the virtual ip of the vpn server
echo "localip 192.168.10.1" >> /etc/pptpd.conf

# Remote vpn client will get IP from this ip pool range
echo "remoteip 192.168.10.10-20" >> /etc/pptpd.conf

#This is to provide DNS , but in this example I really dont need natting for remote vpn client,
#as remote mikrotik will sue this server for radius autehnticaiton only, not as there default gateway🙂
echo "ms-dns 8.8.8.8" >> /etc/pptpd.conf 

Add a TEST account for client in /etc/ppp/chap-secrets

echo "zaibid pptpd zaibpassword *" >> /etc/ppp/chap-secrets

Description:
zaibid = username for remote vpn client
pptpd = service type
zaibpassword = Password for remote vpn client
* = any ip from the pool, if you want to provide same ip to client every time, add ip like 192.168.1.1


TIP:
To Assign static ip to any remote vpn client

To assign static ip to remove vpn client, you can use following

zaibid pptpd zaibpassword 192.168.100.25

Finally restart pptpd service …

/etc/init.d/pptpd restart

Make sure the service is started

service pptpd status

or

ps aux |grep pptpd

If you see it’s running, our Server End is probably OK.


VPN CLIENT in WINDOWS 7

Create a VPN dialer in Windows

Snapshots just for reference …

windows-7-vpn-client


VPN (pptp) CLIENT in Mikrotik RouterOS

vpn-connected-from-mikrotik-client


VPN Server Log Window.

(You can enable log in by uncomment word #debug in /etc/pptpd.conf)

Sep 27 04:15:22 radius pptpd[7582]: MGR: Launching /usr/sbin/pptpctrl to handle client
Sep 27 04:15:22 radius pptpd[7582]: CTRL: local address = 192.168.0.1
Sep 27 04:15:22 radius pptpd[7582]: CTRL: remote address = 192.168.0.234
Sep 27 04:15:22 radius pptpd[7582]: CTRL: pppd options file = /etc/ppp/pptpd-options
Sep 27 04:15:22 radius pptpd[7582]: CTRL: Client X.X.X.X control connection started
Sep 27 04:15:22 radius pptpd[7582]: CTRL: Received PPTP Control Message (type: 1)
Sep 27 04:15:22 radius pptpd[7582]: CTRL: Made a START CTRL CONN RPLY packet
Sep 27 04:15:22 radius pptpd[7582]: CTRL: I wrote 156 bytes to the client.
Sep 27 04:15:22 radius pptpd[7582]: CTRL: Sent packet to client
Sep 27 04:15:22 radius pptpd[7582]: CTRL: Received PPTP Control Message (type: 7)
Sep 27 04:15:22 radius pptpd[7582]: CTRL: Set parameters to 10000000 maxbps, 100 window size
Sep 27 04:15:22 radius pptpd[7582]: CTRL: Made a OUT CALL RPLY packet
Sep 27 04:15:22 radius pptpd[7582]: CTRL: Starting call (launching pppd, opening GRE)
Sep 27 04:15:22 radius pptpd[7582]: CTRL: pty_fd = 6
Sep 27 04:15:22 radius pptpd[7582]: CTRL: tty_fd = 7
Sep 27 04:15:22 radius pptpd[7582]: CTRL: I wrote 32 bytes to the client.
Sep 27 04:15:22 radius pptpd[7582]: CTRL: Sent packet to client
Sep 27 04:15:22 radius pptpd[7583]: CTRL (PPPD Launcher): program binary = /usr/sbin/pppd
Sep 27 04:15:22 radius pptpd[7583]: CTRL (PPPD Launcher): local address = 192.168.0.1
Sep 27 04:15:22 radius pptpd[7583]: CTRL (PPPD Launcher): remote address = 192.168.0.234
Sep 27 04:15:22 radius pppd[7583]: Plugin /usr/lib/pptpd/pptpd-logwtmp.so loaded.
Sep 27 04:15:22 radius pppd[7583]: pppd 2.4.5 started by root, uid 0
Sep 27 04:15:22 radius pppd[7583]: Using interface ppp0
Sep 27 04:15:22 radius pppd[7583]: Connect: ppp0 <--> /dev/pts/1
Sep 27 04:15:22 radius pptpd[7582]: GRE: Bad checksum from pppd.
Sep 27 04:15:22 radius pptpd[7582]: GRE: accepting packet #0
Sep 27 04:15:23 radius pptpd[7582]: GRE: accepting packet #1
Sep 27 04:15:23 radius pptpd[7582]: GRE: accepting packet #2
Sep 27 04:15:23 radius pptpd[7582]: GRE: accepting packet #3
Sep 27 04:15:23 radius pptpd[7582]: GRE: accepting packet #4
Sep 27 04:15:23 radius pppd[7583]: peer from calling number "X.X.X.X" authorized
Sep 27 04:15:23 radius pptpd[7582]: GRE: accepting packet #5
Sep 27 04:15:23 radius pptpd[7582]: GRE: accepting packet #6
Sep 27 04:15:23 radius pptpd[7582]: GRE: accepting packet #7
Sep 27 04:15:23 radius pptpd[7582]: GRE: accepting packet #8
Sep 27 04:15:23 radius pptpd[7582]: GRE: accepting packet #9
Sep 27 04:15:23 radius pptpd[7582]: GRE: accepting packet #10
Sep 27 04:15:23 radius pppd[7583]: MPPE 128-bit stateless compression enabled
Sep 27 04:15:23 radius pptpd[7582]: GRE: accepting packet #11
Sep 27 04:15:23 radius pptpd[7582]: GRE: accepting packet #12
Sep 27 04:15:24 radius pptpd[7582]: GRE: accepting packet #13
Sep 27 04:15:24 radius pptpd[7582]: GRE: accepting packet #14
Sep 27 04:15:24 radius pppd[7583]: Cannot determine ethernet address for proxy ARP
Sep 27 04:15:24 radius pppd[7583]: local IP address 192.168.0.1
Sep 27 04:15:24 radius pppd[7583]: remote IP address 192.168.0.234
Sep 27 04:15:52 radius pptpd[7582]: CTRL: Received PPTP Control Message (type: 5)
Sep 27 04:15:52 radius pptpd[7582]: CTRL: Made a ECHO RPLY packet
Sep 27 04:15:52 radius pptpd[7582]: CTRL: I wrote 20 bytes to the client.
Sep 27 04:15:52 radius pptpd[7582]: CTRL: Sent packet to client
Sep 27 04:15:53 radius pptpd[7582]: GRE: accepting packet #15

Filed under: Linux Related

Linux pppoe client disconnects after x minutes issue

$
0
0

dc


Scenario:

Mikrotik is acting as PPPoE server.

We have one Linux (Ubuntu) base client and wants to connect it to pppoe server using pppoe client. We have created the pppoe dialer using ‘pppoeconfig‘ CLI tool. Its connecting fine using pon dsl-rpovider command.


Problem:

PPPoE Dialer is regularly disconnecting after x minutes and ` /var/log/syslog ` is showing following error

Sep 30 20:40:44 ubuntu pppd[4375]: No response to 4 echo-requests
Sep 30 20:40:44 ubuntu pppd[4375]: Serial link appears to be disconnected.
Sep 30 20:40:44 ubuntu pppd[4375]: Connect time 5.5 minutes.
Sep 30 20:40:44 ubuntu pppd[4375]: Sent 6536 bytes, received 2983 bytes.
Sep 30 20:40:44 ubuntu pppd[4375]: restoring old default route to eth0 [192.168.1.1]
Sep 30 20:40:50 ubuntu pppd[4375]: Connection terminated.
Sep 30 20:40:50 ubuntu pppd[4375]: Modem hangup

Possible Reason:

The other side of the PPP link probably doesn’t support LCP echo. I have seen this with Mikrotik base pppoe server. [zaib]


Solution:

Oddly it was a rear issue i guess and internet is a bit silent about it.

Any way put this line in the relevant ppp options file and try again.

[ Example: /etc/ppp/peers/dsl-provider ]

echo-requests

lcp-echo-interval 0

Save and redial connection by

poff dsl-provider

pon dsl-provider


Complete DSL-PROVIDER file for pppoe client config.


# pppoe dialer config sample, by
# syed jahanzaib

noipdefault
defaultroute
replacedefaultroute
hide-password
lcp-echo-interval 0
noauth
persist
#mtu 1492
#persist
#maxfail 0
#holdoff 20
plugin rp-pppoe.so eth0
usepeerdns
maxfail 0
persist
user "PPPOE-USER-ID"

Regard’s
Syed Jahanzaib


Filed under: Linux Related, Mikrotik Related

Windows batch files to get file/dir size in backup

$
0
0

batch file icon.png

Following are some references to create windows based batch file which performs backups and sends email using windows cmd functions. The task can be achieved by builtin or 3rd party backup applications, but most of the time , there is an Worm which always crawled in my mind and it forces me to do things oddly and above all strangely it somehow works amazingly good with the exact results I want!


Get File Size!

@echo off
rem *** This batch file will list file size ***
 setlocal enableextensions disabledelayedexpansion

rem Change the file name/location in below line
set "file=d:\zaib\setup.exe"

for %%z in ("%file%") do for /f "tokens=1,2" %%a in ('
 robocopy "%%~dpz." "%%~dpz." "%%~nxz" /l /nocopy /is /njh /njs /ndl /nc
 ') do if "%%~dz"=="%%~db" (
 echo "%%~z" : [%%a]
 ) else (
 echo "%%~z" : [%%a%%b]
 )

Result:

1- file size.PNG


Get Folder Size!


@echo off

rem *** This batch file will list FOLDER size ***
setlocal enableextensions disabledelayedexpansion

set "folder=%~f1" & if not defined folder set "folder=%cd%"

set "size=" & for %%z in ("%folder%") do for /f "skip=2 tokens=2,3 delims=: " %%a in ('
robocopy "%%~fz\." "%%~fz\." /l /nocopy /s /is /njh /nfl /ndl /r:0 /w:0 /xjd /xjf /np
^| find ":"
') do if not defined size (
(for /f "delims=0123456789." %%c in ("%%b") do (break)) && (
set "size=%%a%%b"
) || (
set "size=%%a"
)
)

echo "%folder%" : [%size%]

Result:

2- folder size.PNG

 


 

Backup File Example:

I made following batch file long time ago which does the following

  1. Check for MAP drive , if not attache then reconnect it,
  2. If MAP drive is still not available , then break the script and send email to admin
  3. execute oracle exp command to export the DB into the map drive
  4. Delete files older then 15 days to prevent disk fill up
  5. email the result to the admin.

These are just for example only, just to give you an idea only

@echo off
rem # Syed Jahanzaib #
rem # ORACLE-DB SAS BACKUP SCRIPT BY zaib
rem #######################################################
rem Setting various Descriptions via environment variables
rem #######################################################
set dt=%date:~-4,4%%date:~-10,2%%date:~-7,2%
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set ldt=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2%__%ldt:~8,2%-%ldt:~10,2%
set mail-to="ADMINMAIL@GMAIL.COM"
set attachment=C:\backup\last-%ldt%.log
set srvname=ORACLE-DB-SAS.AGP1
set mail-subject=ORACLE-DB-SAS DB %ldt% Dump/Export Report by_Syed_Jahanzaib

break > %attachment%

if exist b:\ (
echo Map Drive is present. Hurraaahhhh zaib you got it Alhamdulillah
) ELSE (
net use B: \\agpinf03\datapark
)

if not exist b:\ (
cho Sorry unable to MAP Drive.
c:\backup\blat\blat.exe %attachment% -to %mail-to% -i %srvname% -s "ERROR UNABLE TO MAP DRIVE - PLEASE CHECK IT. NO BACKUP"
exit /b )

set logpath="B:\ORACLE-DB-sas"
set fname="%logpath%\ORACLE-DB-sas-daily-backup-%ldt%"
echo Executing Backup to DUMP ORACLE-DB-sas Database export Now ...
echo .

exp userid=SAS/ARORACLE-DBS file=%fname% direct=y COMPRESS=y

echo .
echo This script is made by AGP IS Dept. to export daily dump from ORACLE-DB system > %attachment%
echo Please ntoe that Files older then 15 days will be deleted from Fileserver DATAPARK folder %logpath% >> %attachment%
echo Database Export Done. now deleting files older then 15 days
echo .
echo Deleting B:\ORACLE-DB-sas\*.DMP files older then 15 Days from File Server.
echo ****** >> %attachment%
echo Last file name exported is >> %attachment%
forfiles -p "b:\ORACLE-DB-sas" -s -m *.dmp -d 0 -c "cmd /c dir @path" >> %attachment%
echo ****** >> %attachment%
echo File Size is >> %attachment%
ls -lh %fname%.dmp | awk " {print $4;} " >> %attachment%
echo ****** >> %attachment%
echo ****** >> %attachment%
echo Following Files DELETED as per policy if applied >> %attachment%
forfiles -p "b:\ORACLE-DB-sas" -s -m *.dmp -d -15 -c "cmd /c del @path" >> %attachment%
echo ****** >> %attachment%
echo SCRIPT ENDS HERE >> %attachment%
echo powered by Syed Jahanzaib >> %attachment%
echo Done.

c:\backup\blat\blat.exe %attachment% -to %mail-to% -i %srvname% -s "%mail-subject%"

Sample Result:

backup-email-sample


Filed under: Microsoft Related

Cisco Switch Howto Factory Default / Password Recovery

$
0
0

Following is a quick method to reset Cisco switch to factory default settings. This method can also be used to recover password.

Requirements:

  • Cisco Switch
  • Console Cable
  • PC/Laptop with Serial Port

Method#1 – Reset Everything – Factory Default

  • Connect your console cable from the switch Console port to PC/Laptop serial Port.
  • Open the HyperTerminal application, and select settings as per your hardware. Following is one sample for my desktop. Different switches connect with different Bits per Second setting . Try to lower it to 9600 for some models.

hyperterminal.PNG

  • Power Off the switch / Now hold the MODE button on the switch and power ON the switch.

sw-power.jpg

  • Once the switch initialize itself (may take 1-2 minutes), This will interrupt the boot process before the Flash file system can initialize, and after a short while (continue holding the “mode” button) you will see the following prompt:

sw-1

Once you see the

The password-recovery mechanism is enabled.

you can release the Mode button.

  • Now issue following commands to initialize flash and delete files which contains the switch configuration

flash_init
del flash:config.text
del flash:vlan.dat

Once its done, you may continue booting the switch which will allow you to do any configuration of your choice.

 

boot

 

This will boot the switch and all of old / previous settings will be cleared.

If it asks
Would you like to enter the initial configuration dialog? [yes/no]:

Type no and continue with below ..

To add ip use following

en
config t
interface vlan 1
ip address 101.11.100.1 255.0.0.0
ip default-gateway 101.11.100.255
exit
exit
wr

sw-2.PNG

Connect ethernet cable to the switch port and see the ping result.

ping.PNG

 

Now you will be able to connect with the switch using IP.

Cisco configuration assistant software is the best GUI tool to configure the switch with ease and it will save you from remembering all the commands. Although command is more powerful in most cases but still GUI is feasible for most beginners.

cisco-network-gui

You can download Cisco Network Assistant it from my google drive.

https://drive.google.com/drive/folders/0B8B_P2ljEc2xQlpvRUQ2QWVfR0E


 

TIPS:


Add ENABLE section Password

en
config t
enable secret zaib1234
exit
wr

Reboot Switch

wr mem
reload


Vlan1 is administratively down, line protocol is down

en
config t
interface vlan 1
no shut


Enable TELNET access

config
line vty 0 15
transport input telnet
password zaibtelnetpass
login
exit
exit
wr


Enable SSH access

*** Set hostname and domain-name
config t
hostname cisco-switch
ip domain-name zaib.com

*** Generate the RSA Keys
crypto key generate rsa
“How many bits in the modulus [512]: 1024”

*** Set Up the Line VTY configurations
transport input ssh
login local
password zaib1234
exit

*** Set the console line
line console 0
logging synchronous
login local

*** Create the username password
config t
username zaib password zaib1234
enable secret zaib1234
service password-encryption


Method#2 – Password Recovery Only:
(While Keeping existing running configuration intact)

  • Connect Hyperterminal with the switch using console cable. [as mentioned in the beginning of this guide]
  • Power ON the switch while pressing mode button, one you see the “The password-recovery mechanism is enabled.”

More example for switch models:

sw-1

Now issue following commands one by one …

flash_init
dir flash:
rename flash:config.text flash:config.old
(The config.text file contains the password)

Now continue the boot process by following command

boot

Enter n at the prompt to abort the initial configuration dialog.

Now rename the config.old file to config.text so that we can get our existing settings restored , and still let us change the password


rename flash:config.old flash:config.text
Press enter when ask for destination file name

Now save the config in running-config so that switch must load all the settings on next boot

copy flash:config.text system:running-config
Press enter when ask for destination file name

Changing Password …

*** Overwrite the existing secret/password

conf t
enable secret zaib1234
enabled password zaib1234

*** Overwrite the existing vty password

line vty 0 15
password zaib1234

*** Overwrite the existing console password

line con 0
password zaib1234
exit
exit
wr mem
DONE !

Regard’s
Syed Jahanzaib

 

 

 


Filed under: Cisco Related

mysql unable to connect with remote mysql host/database

$
0
0

If you are trying to connect with remote mysql database to perform remote management or connectivity with some web app to add data into the db , and sees the following error

Connection failed: Access denied for user ‘zaib’@’x.x.x.x’ (using password: YES)

OR

Host ‘xxx.xx.xxx.xxx’ is not allowed to connect to this MySQL server

Then you need to grant access to the user from any hostname. This is how you add new privilege from mysql command line.

NOTE: Please beware that this is for just example purpose only , and you MUST take good security measures like Strong Password, and allow only specific IP address to access etc.

 

CREATE USER 'zaib'@'localhost' IDENTIFIED BY 'zaib1234';

GRANT ALL PRIVILEGES ON *.* TO 'zaib'@'localhost' WITH GRANT OPTION;

CREATE USER 'zaib'@'%' IDENTIFIED BY 'zaipassword';

GRANT ALL PRIVILEGES ON *.* TO 'zaib'@'%' WITH GRANT OPTION;

FLUSH PRIVILEGES;


 

Sample PHP page to test Remote Mysql connection

Now you can test it with sample php page. change the host name of remote mysql server, and the id , password we just created in above steps.

 

<?php
$servername = "remote.mysql.host.ip.or.name";
$username = "zaib";
$password = "zaibpass";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully. Hurraahhhhh/Alhamdolillah";
?>

 


Regard’s
Syed Jahanzaib

 


Filed under: Linux Related

Query Windows SAP Server Instance status via Linux

$
0
0

sap.jpg

linux_pengiun

Background:

We are using SAP ECC6 on Windows 2008 R2 64bit server in our data center.
The landscape is as below …

  • PRODUCTION
  • DEV [with 2 instances to support our and parent company as well]
  • Q.A  [with 2 instances to support our and parent company as well]
  • DATA GUARD [For PRD Backup/Replication]

Problem:

During the deployment , we were having strange issue that sometimes any single instance at DEV or QAS stops and we get to know it quite late as it was oftenly happening in saturday sundays when no one is available at data center.


Solution:

Therefore I made the following Linux bash script which performs the following functions >

  1. Check Server PING for its availability
  2. Check SAP Instance using spcontrol command Using WINEXE (Linux utility to execute command on remote windows pc)
  3. If it finds GRAY (DOWN) in the status query, then SEND email / SMS to admin
  4. At next run, check if instance is still down, and the alert is already sent, inform accordingly
  5. At next run, if the instance is UP, then sends alert

Requirements:

  1. Linux (any flavor, I used Ubuntu 12.4)
  2. Winexe (Linux utility to execute command on remote windows pc)
  3. sendEMAIL tool (To send email using your GMAIL account, you can use your own app too)
  4. KANNEL SMS Gateway (To send sms, you can change it , or disable it the script)

TO DO LIST

  • Send alert if server is unreachable
  • Send alert if services like oracle etc are stopped

the SCRiPT ~


#!/bin/bash
# Script to check windows based SAP SERVER INSTANCE status via linux query
# and send sms/email accordingly using winexe/sendEmail/Kannel
# Created by SYED JAHANZAIB / aacable at hotmail dot com
# CREATED: 21-NOV-2016
# LAST REVISION: 22-NOV-2016
#set -x

ESC_SEQ="\x1b["
COL_RESET=$ESC_SEQ"39;49;00m"
COL_RED=$ESC_SEQ"31;01m"
COL_GREEN=$ESC_SEQ"32;01m"
DATE=`date`

# SAP-SERVER RELATED INFO
SAPSRVIP="10.0.0.1"
SAPSRVNAME="SAP - IDES"
DOMAIN="workgroup"
ADMINID="admin"
ADMINPASS="adminpass"

# You must change following path to point sapcontrol.exe
COMPATH="e:\usr\sap\ecc\DVEBMGS00\exe\sapcontrol"

# If you have multiple instance, you may change it as required
INSTANCE="0"

# COMPANY NAME
COMPANY="ZAIB Pvt Ltd."
FOOTER="ZAIB(Pvt) Ltd."

# Hostname
HOSTNAME=`hostname`

# Create /temp folder , if already there remove following entry
mkdir /temp

# KANNEL SMS Gateway Info
KANNELURL="MY.KANNEL.GW:13013"
KANNELID="kannel"
KANNELPASS="KANNELPASS"
# ZAIB CELL
CELL1="03333021909"
SENTSMSRESULT="/temp/$SAPSRVIP.txt"

# GMAIL DETAILS
GMAILID="YOURGMAIL@gmail.com"
GMAILPASS="GMAILPASS"
ADMINMAIL1="aacable @ hotmail . com"
# YOU MUST INSTALL SENDEMAIL TOOL to send email
SENDMAILAPP="/temp/sendEmail-v1.56/sendEmail"
STATUS_HOLDER="/temp/$SAPSRVIP_status.txt"
sapstatus="/temp/$SAPSRVIP_result.txt"
touch $sapstatus

#
#    DO NOT EDIT BELOW THIS POINT, UNLESS YOU WAN TO
# ADD / REMOVE ANY FUNCTION LIKE SMS / EMAIL OR OTHER
#  SYED JAHANZAIB / AACABLE AT HOTMAIL DOT COM

# Check if SAP-SERVER is accessibel or not, if not then EXIT immediately with error / zaib
if [[ $(ping -q -c 1 $SAPSRVIP) == @(*100% packet loss*) ]]; then
echo "ALERT ..... $SAPSRVNAME - IP $SAPSRVIP is now DOWN ..."
exit 1
fi

# If temporary status holder is not present , then create it,
# forumla is being applied to prevent repeated attempt of file creation / zaib
if [ ! -f $STATUS_HOLDER ]; then
echo -e "Creating Status Holder for first time usage"
touch $STATUS_HOLDER
fi

QUERY=`winexe --user=$DOMAIN/$ADMINID%$ADMINPASS //$SAPSRVIP "$COMPATH -nr $INSTANCE -function GetProcessList" |grep GRAY`

NORMSG="$SAPSRVNAME - INSTANCE $INSTANCE QUERY OK !- All Seems OK !"
UPMSG="/temp//$SAPSRVIP.up.sms"
DOWNMSG="/temp/$SAPSRVIP.down.sms"

# MESSAGES
# SMS and email msg fromat for up n down
MSG_UP_SUB="$SAPSRVNAME - $SAPSRVIP - Instance $INSTANCE Restored"
MSG_DOWN_SUB="$SAPSRVNAME - $SAPSRVIP - Instance $INSTANCE IS DOWN"
LOWMSG="$SAPSRVNAME - $SAPSRVIP - Instance $INSTANCE is DOWN

$DATE
$FOOTER"

OKMSG="$SAPSRVNAME - $SAPSRVIP - Instance $INSTANCE is UP now

$DATE
$FOOTER"
# IF SAP INSTANCE QUERY result is GRAY, then send sms and email, IF NOT ALREADY SENT
#if [[ $QUERY =~ .*GRAY.* ]]; then
if [[ $QUERY =~ .*GRAY.* ]]; then
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 1 ]; then
echo "ALERT: $LOWMSG"
echo "$LOWMSG" > $DOWNMSG
echo "SMS/Email for DOWN have already been sent"

fi
fi

if [[ $QUERY =~ .*GRAY.* ]]; then
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 0 ]; then
echo -e "ALERT: $LOWMSG
SENDING SMS/Email .... "
echo "TEMP" > $STATUS_HOLDER

# Sending DOWN SMS via KANNEL
echo $DATE > $SENTSMSRESULT

cat $DOWNMSG | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-
$SENDMAILAPP -u "$MSG_DOWN_SUB" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$DOWNMSG -o message-content-type=text

fi
else
echo -e "$NORMSG ... "
if [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 1 ]; then
echo -e "$COMPANY ALERT : $OKMSG $(date)
SENDING OK UP SMS/Email .... "
echo "$OKMSG" > $UPMSG
sed -i "/TEMP/d" "$STATUS_HOLDER"

# Sending UP SMS via KANNEL
echo $DATE > $SENTSMSRESULT
cat $UPMSG | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-
$SENDMAILAPP -u "$MSG_UP_SUB" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID -o message-file=$UPMSG -o message-content-type=text

fi
fi

# Script Ends Here
# Syed Jahanzaib / aacable @ hotmail . com
# http:// aacable . wordpress . com


Result:

Run the bash script which we created.

CLI RESULT:

sap-status

Email Result:

sap-status-email.PNG

SMS  Result:

screenshot_2016-11-22-14-21-49


 

TIPS n TRICK:

To install sendEmail tool (using GMAIL account), use following

First install Supporting Libraries
For Ubuntu

apt-get -y install libio-socket-ssl-perl libnet-ssleay-perl perl

for centos

yum -y install perl perl-Crypt-SSLeay perl-IO-Socket-SSL

Now Download and unzip the sendEmail tool

mkdir /temp
cd /temp
wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
tar zxvf sendEmail-v1.56.tar.gz
cd sendEmail-v1.56/

To test use following

/temp/sendEmail-v1.56/sendEmail -t DESTINATIONMAIL@hotmail.com -u "Test Email" -s smtp.gmail.com:587 -xu YOURGMAILID@gmail.com -xp GMAILPASS -f YOURGMAILID -o tls=yes -m "hi"
Nov 22 15:16:46 linux sendEmail[12561]: Email was sent successfully!

to install WINEXE on Ubuntu 12.4 [zaib]

mkdir /temp && cd /temp
apt-get install python-all-dev
wget http://downloads.sourceforge.net/project/winexe/winexe-1.00.tar.gz
tar xzvf winexe-1.00.tar.gz
cd winexe-1.00/source4/
./autogen.sh
./configure
make basics bin/winexe
./bin/winexe -V
# copy winexe binary to /usr/sbin   so that it can be called from any path
# cp /temp/winexe-1.00/source4/bin  /usr/sbin

To query Remote SAP Server instance status use following command

*Make sure you change the path of sapcontrol path/folder to match with your local installation folder structure

# Remote Server with workgroup/standalone
/temp/winexe-1.00/source4/bin/winexe -U ADMINID%PASSWORD //10.0.0.1 "E:\usr\sap\ECC\DVEBMGS00\exe\sapcontrol -nr 0 -function GetProcessList"

# Remote Server with DOMAIN base authentication
winexe --user=DOMAINNAME/ADMINID%PASSWORD //10.0.0.1 "e:\usr\sap\R3d\DVEBMGS00\exe\sapcontrol -nr 0 -function GetProcessList"

To query Remote windows services status , use following command

install the require tool by following

sudo apt-get install samba-common

List all services on remote windows server by following

net rpc service list -I 10.0.0.1 -U DOMAIN/ADMINID%PASSWORD

now query the service STATUS with following

net rpc service status OracleServiceR3D -I 10.0.0.1 -U DOMAIN/ADMINID%PASSWORD

net rpc service status OracleServiceR3D -I 10.0.0.1 -U DOMAIN/ADMINID%PASSWORD | grep running

to stop or start

net rpc service stop SERVICENAME -I IPADDRESS -U USERNAME%PASSWORD
net rpc service start SERVICENAME -I IPADDRESS -U USERNAME%PASSWORD

 

Regard’s
SYED JAHANZAIB


Filed under: Linux Related, Microsoft Related

Mikrotik: Switching between interface

$
0
0

f

NOT FOR GENERAL PUBLIC

SHARING IT FOR PERSONAL REFERENCE


Following is an script that switches connectivity between two interfaces (usually wan).

Example: ISP x have provided internet connectivity via fiber link . as a backup / redundancy link ISP have provided second fiber line. Both link have same ip address with same gateway. There are other ways too , this is just one way of achieving task. it can be customized in many ways like forcing specific hosts to be monitored by ping and act accordingly , or by having bridge etc.

IP address example:

in /ip address >>>

sfp1 = 10.0.0.1 (with comments “PRIMARY-SFP1”)
[remains enabled until next status changed]

sfp2 = 10.0.0.1 (with comments “SECONDARY-SFP2”)
[remains enabled until next status changed]

The script will do following.

Check the SFP1 Status, if it does not found “linkok” reply, it will log event, send email/sms alert to admin, and switch to SFP2 port by enabling IP of SFP2 and enable ip on SFP1.

On next run, if it found SFP1 link ok, then it will log event, send email/sms alert to admin, and switch back to SFP1 port by enabling IP of SFP1 and disable ip on SFP2.


# Mikrotik SPF Link monitoring SCRIPT with optional Email and SMS Alert,
# We are using local KANNEL as SMS gateway and GMAIL as email sender
# by Syed Jahanzaib
# https://aacable.wordpress.com
# Email : aacable at hotmail dot com
# Script Last Modified : 25-NOV-2016 / 1400 hours

# PRIMARY FIBER LINK
:local INT "sfp1"
# SECONDARY BACKUP FIBER LINK
:local INT2 "sfp2"
:local i 0;
:local F 0;
:local date;
:local time;
:global sub1 ([/system identity get name])
:global sub2 ([/system clock get time])
:global sub3 ([/system clock get date])
:set date [/system clock get date];
:set time [/system clock get time];
:local cell1 "03333021909"
:global FIBERnetstatus;
:global FIBERnetlastchange;
:global FIBERIP;

# Company Name, do not use spaces in it
:local COMPANY "ZZZ"

# Setting GMAIL config
:local sub1 ([/system identity get name])
:local sub2 ([/system clock get date])
:local sub3 ([/system clock get time])
:local TO1 "RECIPIENT-1@hotmail.com"
:local gmailid "YOUR-GMAIL-ID@gmail.com"

#If you dont have kannel sms gateway ignore this.
:local KURL "http://10.0.0.1:13013/cgi-bin/sendsms"
:local KID "kannel"
:local KPASS "KANNEL-PASS"

# SMS DOWN status Msg format for Kannel SMS gateway (donot use spaces in it)
:local MSGDOWNSMS "$COMPANY+FIBER+ALERT:%0A$INT+fiber+is+now+DOWN.%0ASwitching+to+$INT2+backup+fiber+link"
# SMS UP status Msg format for Kannel SMS gateway (donot use spaces in it)
:local MSGUPSMS "$COMPANY+FIBER+INFO:%0A$INT+fiber+is+now+UP.%0ASwitching+back+to+$INT"

# EMAIL Msg format for FIBER DOWN
:local MSGDOWNEMAIL "$COMPANY FIBER ALERT: $INT fiber is now DOWN at $sub1 $sub2 $sub3 . Switching to $INT2 backup link. Please Verify it."
# EMAIL Msg format for FIBER UP
:local MSGUPEMAIL "$COMPANY FIBER INFO: $INT fiber is now UP at $sub1 $sub2 $sub3 . Switching back to $INT primary link. Please Verify it."
# LOG error
:local DOWNLOG1 "$COMPANY FIBER ALERT: $INT fiber is now DOWN at $sub1 $sub2 $sub3 . Switching to $INT2 backup link. Please Verify it."
:local UPLOG1 "$COMPANY FIBER INFO: $INT fiber is now UP at $sub1 $sub2 $sub3 . Switching back to $INT primary link. Please Verify it"

######################################
########## Start the SCRIPT ###############
########## DONOT EDIT BELOW ############
######################################

local link;
/interface ethernet cable-test $INT once do={
:set link $"status";
};

:if ($link != "link-ok") do={
:log error "$INT SFP DOWN"

:if (($FIBERnetstatus="UP")) do={
:set FIBERnetstatus "DOWN";

# Also add status in global variables to be used as tracking
:set date [/system clock get date];
:set time [/system clock get time];
:set FIBERnetlastchange ($time . " " . $date);
##################################################
####### FOR DOWN STATUS, CHANGE THE RULES ########
##################################################

# If the link is down, then LOG info and warning in Mikrotik LOG window [Zaib]
:log error "$DOWNLOG1"

# DOWN ACTION , shifting to SFP2 , backup link
/ip address set disabled=yes [find comment="PRIMARY-SFP1"]
/ip address set disabled=no [find comment="BACKUP-SFP2"]

# Adding delay so gateway should be reachable properly
:delay 5s;

:global gmailsmtp
:set gmailsmtp [:resolve "smtp.gmail.com"];

# "Emailing the DOWN status. . . "
/tool e-mail send to=$TO1 subject="$MSGDOWNEMAIL" start-tls=yes body="$MSGDOWNEMAIL"

# Send SMS the DOWN status via KANNEL
/tool fetch url="$KURL\?username=$KID&password=$KPASS&to=$cell1&text=$MSGDOWNSMS"

##################################################
####### FOR UP STATUS, CHANGE THE RULES ########
##################################################

} else={:set FIBERnetstatus "DOWN";}
} else={
:if (($FIBERnetstatus="DOWN")) do={

:set FIBERnetstatus "UP";

# If link is UP, then LOG info and warning in Mikrotik LOG window [Zaib]
:log warning "$UPLOG1"

# UP ACTION , shifting back to SFP1
/ip address set disabled=no [find comment="PRIMARY-SFP1"]
/ip address set disabled=yes [find comment="BACKUP-SFP2"]

# Adding delay so gateway should be reachable properly
:delay 5s;
:global gmailsmtp
:set gmailsmtp [:resolve "smtp.gmail.com"];

# "Emailing the UP status. . . "
/tool e-mail send to=$TO1 subject="$MSGUPEMAIL" start-tls=yes body="$MSGUPEMAIL"

# Send SMS via KANNEL Gateway
/tool fetch url="$KURL\?username=$KID&password=$KPASS&to=$cell1&text=$MSGUPSMS"

:set date [/system clock get date];
:set time [/system clock get time];
:set FIBERnetlastchange ($time . " " . $date);

} else={:set FIBERnetstatus "UP";}
}


Filed under: Mikrotik Related, Uncategorized

Kannel SMS gateway with multiple smsc/modems & CMS Err 330

$
0
0

kannellogo

 

Following are reference guide on how you can run Kannel with multiple modems / smsc.
For basic / initial level of KANNEL configuration, please refer to the internet or my previous post at …

https://aacable.wordpress.com/2012/11/26/howto-configure-your-local-http-gateway-using-kannel-on-ubuntu/


Scenario:

Kannel is working as an SMS gateway to send/receive SMS. There is one Serial Modem connected with it using ttyS0 port. Another USB GSM Modem have been added recently which is installed as ttyUSB2.

Now we want to send SMS some specific announcement base messages via Modem#1 and some complaint system messages via Modem#2.


Hardware Used:

OS = Ubuntu 12.4 / Server Edition / x86 [Its support may end in 2017]

SMS Gateway App = Kannel 1.4.3

Modems:

1# Teltonika Modem Model = ModemCOM-G10 [Reliable & solid in performance, serial modems are good and easily configurable]
http://www.teltonika.lt/product/modemcom-g10/

modemcom-g10

 

2# TP-Link MA260 USB GSM Modem [Problematic, avoid using such cheap brands like tplink/dlink etc]
http://www.tp-link.com/en/products/details/cat-5090_MA260.html

 

ma260-01


KANNEL.CONF  Sample


# KANNEL config sample for multiple modem / SMSC
# Syed Jahanzaib

# MODEM 1 / Teltonika Brand / Type : Serial
group = smsc
smsc = at
smsc-id = zaibgsmid1
modemtype = teltonika
device = /dev/ttyS0
transceiver-mode = true
speed = 115200
# MODEM 2 / TP-LINK MA260 Brand / Type : USB
group = smsc
smsc = at
smsc-id = zaibgsmid2
modemtype = teltonika
device = /dev/ttyUSB2
transceiver-mode = true
speed = 115200
# for other models/modems adjust the INIT strings accordingly
group = modems
id = teltonika
name = "Teltonika E12"
detect-string = "Undefined"
enable-mms = true
#init-string = "AT+CNMI=2,2,0,1,1"
init-string = "ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0;+CNMI=1,3,2,2,1;+CMEE=1;+CPMS="SM","SM","SM";+CMGF=0"
speed = 115200
message-storage = "SM"

Make sure to restart the Kannel service and watch the bearerbox.log available at /var/log/kannel for any errors.


Howto send SMS via specific SMSC / MODEM

Now to send SMS using HTTP API method , via modem # 1 or 2, we will use its smsc ID as shown in example below … focus on smsc= part

MODEM#1

http://KANNEL-IP:13013/cgi-bin/sendsms?smsc=zaibgsmid1&username=KANNELID&password=KANNEL-PASSWORD&to=MOBILENO&text=TEST+MSG+FROM+MODEM+1

MODEM#2

http://KANNEL-IP:13013/cgi-bin/sendsms?smsc=zaibgsmid2&username=KANNELID&password=KANNEL-PASSWORD&to=MOBILENO&text=TEST+MSG+FROM+MODEM+2

 


CMS ERROR 330

I was getting cms error 330 in bearerbox.log when sending SMS with specific model of TP-LINK MA260 ,
As shown below …

cms-error

2016-12-01 08:24:05 [13727] [6] DEBUG: AT2[zaibgsmid1]: <– +CMS ERROR: 330
2016-12-01 08:24:05 [13727] [6] ERROR: AT2[zaibgsmid1]: CMS ERROR: +CMS ERROR: 330
2016-12-01 08:24:05 [13727] [6] ERROR: AT2[zaibgsmid1]: CMS ERROR: SMSC address unknown (330)

With some troubleshooting using minicom, I found out that the message center service number was not set, Strange, it should get the value auto (which is normal with other modems) , any way I sort this issue as following

(For ZONG SIM)

AT+CSCA=”+923040000011″

# now Query CSCA number

AT+CSCA?

and you should get the center number fine.

Now send sms using minicom AT commands

# Sets text mode
AT+CMGF=1  
# Now input the msg we want to send to recipient
AT+CMGS=”03333021909″
# Press Enter, and type your desired msg
TEST MSG FROM ZAIB
# Press CTRL+Z and it will send the SMS to the specified number

Some other useful minicom / AT commands with minicom

# To configure Modem ports/etc
minicom -s

General Check
AT

Getting IMEI
AT+CGSN
AT+CIMI

Getting Signal quality and battery charge status
AT+CSQ
AT+CBC

Model Inquiry
AT+CGMI

MINICOM USSD
T+CUSD=1,”your_service_code”,15

so for your case

AT+CUSD=1,”*555*87*1234#
and don’t forget the 15 at the end , this question helped me to solve the problem.


Regard’s
Syed Jahanzaib


Filed under: Linux Related

rrdtool: MRTG next level graphing

$
0
0

DRAFT VERSION: This is incomplete Post ! Some points may be missing, I will update them later ..

rrdtool

We all know what is MRTG. You can graph so many information including temperature humidity, speed, voltage, uptime, routers, active numbers of users,  number of printouts and sometimes out of the box graphing/zaib.

In this post, I am just posting reference material on how to move plain mrtg graphs to RRD. The advantage of RRD over MRTG is that using RRD you can generate the graphs on the fly, as opposed as MRTG that generates the graphs all the time, no matter that nobody sees the generated HTML pages or not. we can use RRD to make graphs with one minute interval, whereas in plain MRTG, we have to use 5 minutes interval which is not good to monitor jerks or sensitive devices.

For initial / basic level of MRTG installation on Ubuntu Server , refer to the inernet or my previous post at

https://aacable.wordpress.com/tag/mrtg-installation-on-ubuntu/


Sample CFG Files:

main MRTG.CFG file

cat /etc/mrtg.cfg


#########################
# START OF /etc/mrtg.cfg#
#########################
# Author: Syed Jahanzaib
# Email : aacable@hotmail.com
# Web : https://aacable.wordpress.com

WorkDir: /var/www/mrtg
Options[_]: growright,nobanner,pngdate,noinfo,nobanner

XSize[_]: 600
YSize[_]: 200
EnableIPv6: no
RunAsDaemon: no
Interval: 1
Logformat: rrdtool
#Use MIBS as per your local config, make sure you download the mibs as mentioned here
# https://aacable.wordpress.com/tag/mrtg-installation-on-ubuntu/
LoadMIBs: /cfg/mibs/HOST-RESOURCES-MIB /cfg/mibs/IF-MIB /cfg/mibs/UCD-SNMP-MIB
PageFoot[^]: Page managed by SYED JAHANZAIB
AddHead[_]:
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cstyle%20type%3D%22text%2Fcss%22%3E%20%3C!.%20a%20%7B%20color%3A%20%23263F66%3B%20text-decoration%3A%20none%3B%20%7D%20a%3Ahover%20%7B%20color%3A%20%23785B41%3B%20text-decoration%3A%20none%3B%20%7D%20body%20%7B%20color%3A%20black%3B%20font%3A%208pt%20Verdana%2C%20Geneva%2C%20Arial%2C%20Helvetica%2C%20sans-serif%3B%20%7D%20h1%20%7B%20font%3A%20bold%2016pt%20Verdana%2C%20Geneva%2C%20Arial%2C%20Helvetica%2C%20sans-serif%3B%20color%3A%20%23342A21%3B%20%7D%20h2%20%7B%20color%3A%20%23666666%3B%20font%3A%20bold%2012pt%20Verdana%2C%20Geneva%2C%20Arial%2C%20Helvetica%2C%20sans-serif%3B%20%7D%20h3%20%7B%20color%3A%20black%3B%20font%3A%20bold%209pt%20Verdana%2C%20Geneva%2C%20Arial%2C%20Helvetica%2C%20sans-serif%3B%20%7D%20table%20%7B%20border%3A%200%3B%20%7D%20td%20%7B%20background-color%3A%20%23E7DDD3%3B%20border%3A%200px%20solid%20%23FFFFFF%3B%20color%3A%20Black%3B%20font%3A%208pt%20Verdana%2C%20Geneva%2C%20Arial%2C%20Helvetica%2C%20sans-serif%3B%20vertical-align%3A%20top%3B%20%7D%20th%20%7B%20background-color%3A%20%23735A4A%3B%20color%3A%20White%3B%20font%3A%2011px%20Verdana%2C%20arial%2C%20geneva%2C%20helvetica%2C%20sans-serif%3B%20border%3A%200%3B%20font-weight%3A%20bold%3B%20text-align%3A%20left%3B%20%7D%20.%3E%20%3C%2Fstyle%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;style&gt;" title="&lt;style&gt;" />

Background[_]: #F6F1EE

# Change it as required , it means mrtg will read following file and will create graphs based on its contents
# ADD remove them as required, they are added for reference purposes only
#Include: /cfg/temp.cfg
Include: /cfg/kesc.cfg
Include: /cfg/lanwan.cfg
Include: /cfg/radius.cfg
Include: /cfg/mt.cfg
Include: /cfg/ping.cfg
#Include: /cfg/vlan.cfg

##################
# END OF /etc/mrtg.cfg #
##################


radius.cfg


# Created by
# /usr/bin/cfgmaker public@10.0.0.1

# or for NT
WorkDir: /var/www/mrtg

### Interface 2 >> Descr: 'eth0' | Name: 'eth0' | Ip: '1' | Eth: '00' ###

Target[10.0.0.1_eth0]: #eth0:public@10.0.0.1:
SetEnv[10.0.0.1_eth0]: MRTG_INT_IP="10.0.0.1" MRTG_INT_DESCR="eth0"
MaxBytes[10.0.0.1_eth0]: 125000000
Title[10.0.0.1_eth0]: Traffic Analysis for eth0 -- BILLING
PageTop[10.0.0.1_eth0]:
<h1>Traffic Analysis for eth0 -- BILLING</h1>
<div id="sysdetails">
<table>
<tr>
<td>System:</td>
<td>in "BILLING"</td>
</tr>
<tr>
<td>Maintainer:</td>
<td>aacable at hotmail dot com</td>
</tr>
<tr>
<td>Description:</td>
<td>eth0</td>
</tr>
<tr>
<td>ifType:</td>
<td>ethernetCsmacd (6)</td>
</tr>
<tr>
<td>ifName:</td>
<td>eth0</td>
</tr>
<tr>
<td>Max Speed:</td>
<td>125.0 MBytes/s</td>
</tr>
<tr>
<td>Ip:</td>
<td>10.0.0.1 (click.onmypc.net)</td>
</tr>
</table>
</div>
#Percent of memory used
Target[radius_server_mem_ram]: ( hrStorageUsed.1&hrStorageUsed.1:public@10.0.0.1) * 100 / ( hrStorageSize.1&hrStorageSize.1:public@10.0.0.1 ) / 10
Title[radius_server_mem_ram]: Memory usage for Radius Server
PageTop[radius_server_mem_ram]:
<H1> Memory usage for Radius Server </H1>

MaxBytes[radius_server_mem_ram]: 100
AbsMax[radius_server_mem_ram]: 100
ShortLegend[radius_server_mem_ram]: %
YLegend[radius_server_mem_ram]: % of Memory
Legend1[radius_server_mem_ram]: Used Memory
LegendI[radius_server_mem_ram]: Used :
LegendO[radius_server_mem_ram]:
Options[radius_server_mem_ram]: growright,nopercent,gauge,integer,nobanner,printrouter,pngdate,noo
#Unscaled[radius_server_mem_ram]: ymwd

Target[linux_server_dis_usage]: ( hrStorageUsed.31&hrStorageUsed.31:public@10.0.0.1 ) * 100 / ( hrStorageSize.31&hrStorageSize.31:public@10.0.0.1 ) / 8
Title[linux_server_dis_usage]: Disk usage for Linux Server
PageTop[linux_server_dis_usage]:
<H1> Disk usage for Linux Server </H1>

MaxBytes[linux_server_dis_usage]: 100
AbsMax[linux_server_dis_usage]: 100
ShortLegend[linux_server_dis_usage]: %
YLegend[linux_server_dis_usage]: % of RM_DISK
Legend1[linux_server_dis_usage]: Used RM_DISK
LegendI[linux_server_dis_usage]: Used :
LegendO[linux_server_dis_usage]:
Options[linux_server_dis_usage]: growright,gauge,integer,nobanner,printrouter,pngdate,noo,nopercent,logscale
#Unscaled[linux_server_dis_usage]: ymwd


Mikrotik Sample CFG File


workdir: /var/www/mrtg/
#Options[_]: growright,nopercent

# Z_CCR_MIKROTIK CCR_1036 PPPoE ACTIVE Users
Target[mtPPPoEusers]: 1.3.6.1.4.1.9.9.150.1.1.1.0&1.3.6.1.4.1.9.9.150.1.1.1.0:public@10.0.0.1 / 8
Title[mtPPPoEusers]: Active PPPoE Users on Z_CCR_MIKROTIK CCR_1036
PageTop[mtPPPoEusers]:
<H1>Active PPPoE Users on Z_CCR_MIKROTIK CCR_1036</H1>

MaxBytes[mtPPPoEusers]: 2000
Colours[mtPPPoEusers]: B#8888ff,B#8888ff,B#5398ff,B#5398ff
Options[mtPPPoEusers]: growright,nopercent,gauge,integer,nobanner,printrouter,pngdate,noo
LegendI[mtPPPoEusers]: Active PPPoE Users on MT
LegendO[mtPPPoEusers]:
YLegend[mtPPPoEusers]: Active PPPoE Users on MT
Legend1[mtPPPoEusers]: Active PPPoE Users on MT
Legend2[mtPPPoEusers]:
#Unscaled[mtPPPoEusers]: dwmy

### Z_CCR_MIKROTIK CCR_1036 192.168.100.2 CPU load ###
Target[192.168.100.2_cpu]: 1.3.6.1.2.1.25.3.3.1.2.1&1.3.6.1.2.1.25.3.3.1.2.1:public@10.0.0.1 / 8
AbsMax[192.168.100.2_cpu]: 100
MaxBytes[192.168.100.2_cpu]: 100
Title[192.168.100.2_cpu]: Z_CCR_MIKROTIK CCR_1036 CPU load
PageTop[192.168.100.2_cpu]:
<H1>Z_CCR_MIKROTIK CCR_1036 CPU load</H1>

Options[192.168.100.2_cpu]: growright,gauge,integer,nobanner,printrouter,pngdate,noo,nopercent
Legend1[192.168.100.2_cpu]: CPU load
YLegend[192.168.100.2_cpu]: CPU load
ShortLegend[192.168.100.2_cpu]: %
LegendI[192.168.100.2_cpu]: CPU load (percentage)
#Unscaled[192.168.100.2_cpu]: y

### MONITORING Z_CCR_MIKROTIK CCR_1036 CPU TEMP ###
Target[mt.cpu.temp]: 1.3.6.1.4.1.14988.1.1.3.11.0&1.3.6.1.4.1.14988.1.1.3.11.0:public@10.0.0.1 / 100
Options[mt.cpu.temp]: gauge, growright, nopercent, noinfo
MaxBytes[mt.cpu.temp]: 100
Colours[mt.cpu.temp]: RED#ff4f27,Y#fffb15,RED#ff4f27,RED#fffb15
#Unscaled[mt.cpu.temp]: dwmy
YLegend[mt.cpu.temp]: CPU Temprature
Title[mt.cpu.temp]: Z_CCR_MIKROTIK CCR_1036 RB CPU Temprature
PageTop[mt.cpu.temp]:
<H1>Z_CCR_MIKROTIK CCR_1036 RB CPU Temprature</H1>

ShortLegend[mt.cpu.temp]: c
LegendI[mt.cpu.temp]:
LegendO[mt.cpu.temp]: CPU Temp
Legend1[mt.cpu.temp]: CPU Temprature
Legend2[mt.cpu.temp]: CPU Temprature

### MONITORING Z_CCR_MIKROTIK CCR_1036 RB TEMP ###
Target[mt.rb.temp]: 1.3.6.1.4.1.14988.1.1.3.10.0&1.3.6.1.4.1.14988.1.1.3.10.0:public@10.0.0.1 / 100
Options[mt.rb.temp]: gauge, growright, nopercent, noinfo
MaxBytes[mt.rb.temp]: 100
Colours[mt.rb.temp]: RED#ff4f27,Y#fffb15,RED#ff4f27,RED#fffb15
#Unscaled[mt.rb.temp]: dwmy
YLegend[mt.rb.temp]: RB Temprature
Title[mt.rb.temp]: Z_CCR_MIKROTIK CCR_1036 Router Board Temprature
PageTop[mt.rb.temp]:
<H1>Z_CCR_MIKROTIK CCR_1036 Router Board Temprature</H1>

ShortLegend[mt.rb.temp]: c
LegendI[mt.rb.temp]:
LegendO[mt.rb.temp]: RB Temp
Legend1[mt.rb.temp]: RB Temprature
Legend2[mt.rb.temp]: RB Temprature

### MONITORING Z_CCR_MIKROTIK CCR_1036 VOLTAGE Monitor ###
Target[mt-voltage]: 1.3.6.1.4.1.14988.1.1.3.8.0&1.3.6.1.4.1.14988.1.1.3.8.0:public@10.0.0.1 / 8
Options[mt-voltage]: growright,nopercent,gauge,integer,nobanner,printrouter,pngdate
MaxBytes[mt-voltage]: 30
Colours[mt-voltage]: RED#ff4f27,Y#fffb15,RED#ff4f27,RED#fffb15
Unscaled[mt-voltage]: dwmy
YLegend[mt-voltage]: Voltaeg Monitor
Title[mt-voltage]: Z_CCR_MIKROTIK CCR_1036 Cloudcore RB VOLTAGE Monitor
PageTop[mt-voltage]:
<H1>Z_CCR_MIKROTIK CCR_1036 Cloudcore RB VOLTAGE Monitor</H1>

ShortLegend[mt-voltage]: v
LegendI[mt-voltage]:
LegendO[mt-voltage]: Voltage
Legend1[mt-voltage]: Voltage
Legend2[mt-voltage]: Voltage

### MONITORING Z_CCR_MIKROTIK CCR_1036 power.consumption Watt Usage Monitor ###
Target[mt-powerconsumption]: .1.3.6.1.4.1.14988.1.1.3.12.0&.1.3.6.1.4.1.14988.1.1.3.12.0:public@10.0.0.1 / 100
Options[mt-powerconsumption]: gauge, growright, nopercent, noinfo
MaxBytes[mt-powerconsumption]: 1000
Colours[mt-powerconsumption]: RED#ff4f27,Y#fffb15,RED#ff4f27,RED#fffb15
#Unscaled[mt-powerconsumption]: dwmy
YLegend[mt-powerconsumption]: Watts USAGE Monitor
Title[mt-powerconsumption]: Z_CCR_MIKROTIK CCR_1036 Cloudcore RB Watts Usage Monitor
PageTop[mt-powerconsumption]:
<H1>Z_CCR_MIKROTIK CCR_1036 Cloudcore RB Watts Usage Monitor</H1>

ShortLegend[mt-powerconsumption]: W
LegendI[mt-powerconsumption]:
LegendO[mt-powerconsumption]: power.consumption.wts
Legend1[mt-powerconsumption]: power.consumption.wts
Legend2[mt-powerconsumption]: power.consumption.wts

### MONITORING Z_CCR_MIKROTIK CCR_1036 FAN-2 Speed Monitor ###
Target[mt-FAN2-speed]: 1.3.6.1.4.1.14988.1.1.3.18.0&1.3.6.1.4.1.14988.1.1.3.18.0:public@10.0.0.1 / 8
Options[mt-FAN2-speed]: growright,nopercent,gauge,integer,nobanner,printrouter,pngdate,logscale,noi
MaxBytes[mt-FAN2-speed]: 10000
Colours[mt-FAN2-speed]: RED#ff4f27,Y#fffb15,RED#ff4f27,RED#fffb15
#Unscaled[mt-FAN2-speed]: dwmy
YLegend[mt-FAN2-speed]: FAN-2 RPM
Title[mt-FAN2-speed]: Z_CCR_MIKROTIK CCR_1036 Cloudcore RB FAN-2 Speed Monitor
PageTop[mt-FAN2-speed]:
<H1>Z_CCR_MIKROTIK CCR_1036 Cloudcore RB FAN-2 Speed Monitor</H1>

ShortLegend[mt-FAN2-speed]: RPM
LegendI[mt-FAN2-speed]:
LegendO[mt-FAN2-speed]: fan-2.rpm.speed
Legend1[mt-FAN2-speed]: fan-2.rpm.speed
Legend2[mt-FAN2-speed]: fan-2.rpm.speed

### MONITORING Z_CCR_MIKROTIK CCR_1036 AMP Monitor ###
Target[mt-amp-mon]: 1.3.6.1.4.1.14988.1.1.3.13.0&1.3.6.1.4.1.14988.1.1.3.13.0:public@10.0.0.1 / 10000
Options[mt-amp-mon]: gauge,growright,nopercent,pngdate,nobanner
MaxBytes[mt-amp-mon]: 30
Colours[mt-amp-mon]: RED#ff4f27,Y#fffb15,RED#ff4f27,RED#fffb15
#Unscaled[mt-amp-mon]: dwmy
YLegend[mt-amp-mon]: AMP USAGE Monitor
Title[mt-amp-mon]: Z_CCR_MIKROTIK CCR_1036 Cloudcore RB AMP Monitor
PageTop[mt-amp-mon]:
<H1>Z_CCR_MIKROTIK CCR_1036 Cloudcore RB AMP Monitor</H1>

ShortLegend[mt-amp-mon]: amp
LegendI[mt-amp-mon]:
LegendO[mt-amp-mon]: amps
Legend1[mt-amp-mon]: amps
Legend2[mt-amp-mon]: amps


ping.cfg


WorkDir: /var/www/mrtg
### ping.cfg
# 10.0.0.1 MIKROTIK NAS SERVERPING GRAPH
Title[mikrotik.ping]: MIKROTIK NAS SERVER PING RTT / Pkt LOSS Report
PageTop[mikrotik.ping]:
<H1>MIKROTIK NAS SERVER PING RTT / Pkt LOSS Report</H1>

Target[mikrotik.ping]: `/cfg/norping.sh 10.0.0.1`
Colours[mikrotik.ping]: R#f75712,G#04bf27,G#70ff53,R#70ff53
MaxBytes[mikrotik.ping]: 100
AbsMax[mikrotik.ping]: 100
Options[mikrotik.ping]: growright,gauge,pngdate,printrouter,nopercent,noinfo,logscale
#growright,nopercent,gauge
LegendI[mikrotik.ping]: Pkt LOSS
LegendO[mikrotik.ping]: Active Round Trip Time
YLegend[mikrotik.ping]: RTT
Legend1[mikrotik.ping]: Pkt LOSS
Legend2[mikrotik.ping]: Active RTT

# 8.8.8.8 GOOGLE SERVERPING GRAPH
Title[8.8.8.8.ping]: GOOGLE DNS PING RTT / Pkt LOSS Report
PageTop[8.8.8.8.ping]:
<H1>GOOGLE DNS PING RTT / Pkt LOSS Report</H1>

Target[8.8.8.8.ping]: `/cfg/norping.sh 8.8.8.8`
Colours[8.8.8.8.ping]: R#f75712,G#04bf27,G#70ff53,R#70ff53
MaxBytes[8.8.8.8.ping]: 100
AbsMax[8.8.8.8.ping]: 100
Options[8.8.8.8.ping]: growright,gauge,pngdate,printrouter,nopercent,noinfo,logscale
#growright,nopercent,gauge
LegendI[8.8.8.8.ping]: Pkt LOSS
LegendO[8.8.8.8.ping]: Active Round Trip Time
YLegend[8.8.8.8.ping]: RTT
Legend1[8.8.8.8.ping]: Pkt LOSS
Legend2[8.8.8.8.ping]: Active RTT


kesc.cfg


WorkDir: /var/www/mrtg

# 10.0.0.2 MIKROTIK AP FOR KESC RPING GRAPH
Title[kesc.ping]: K.E.S.C LIGHT FAILURE GRAPH / MRTG GRAPH
PageTop[kesc.ping]:
<H1>K.E.S.C LIGHT FAILURE / MRTG GRAPH Report ! z@iB</H1>

Target[kesc.ping]: `/cfg/kping.sh 10.0.0.2`
Colours[kesc.ping]: RED#ff4f27,GREEN#00eb0c,RED#ff4f27,RED#fffb15
MaxBytes[kesc.ping]: 100
AbsMax[kesc.ping]: 100
Options[kesc.ping]: growright,gauge,pngdate,printrouter,nopercent,noinfo,logscale
ShortLegend[kesc.ping]: %
LegendI[kesc.ping]: K.E Light Loss
LegendO[kesc.ping]: K.E Availability
YLegend[kesc.ping]: Red Bar Shows Failure (:)


norping.sh


#!/bin/bash
HOST=$1
ping -c 3 $HOST > /dev/null
DATA=`ping -c 3 $1 -q `
if [ $? -eq 0 ]
then
echo "0"
echo $DATA | awk -F/ '{print $6 }'

else
echo "100
0"
fi


APACHE / WEB SERVER Section

To enable cgi execution in /var/www/mrtg folder, edit the apache config file, and

nano /etc/apache2/sites-enabled/000-default

and paste following so that the last section of file should be like below …


<Directory /var/www/mrtg>
AddHandler cgi-script .cgi
Options +ExecCGI +Indexes
DirectoryIndex index.cgi
</Directory>
</VirtualHost>


mrtg-rrd.cgi or index.cgi to graph creation

Following CGI file will create MRTG graph on the fly (on demand)


#!/usr/bin/perl -w
# $Id: mrtg-rrd.cgi,v 1.35 2003/08/18 15:58:57 kas Exp $
# mrtg-rrd.cgi: The script for generating graphs for MRTG statistics.
# Loosely modelled after the Rainer.Bawidamann@informatik.uni-ulm.de's
# 14all.cgi
use strict;
use POSIX qw(strftime);
use Time::Local;
# The %.1f should work on 5.005+. There may be other problems, though.
# I've tested this on 5.8.0 only, so mind the gap!
require 5.005;
# Location of RRDs.pm, if it is not in @INC
# use lib '/usr/lib/perl5/5.00503/i386-linux';
use RRDs;
use vars qw(@config_files @all_config_files %targets $config_time
%directories $version $imagetype);

# EDIT THIS to reflect all your MRTG config files
BEGIN { @config_files = qw(/etc/mrtg.cfg); }

$version = '0.7';
# This depends on what image format your libgd (and rrdtool) uses
$imagetype = 'png'; # or make this 'gif';
# strftime(3) compatibility test
my $percent_h = '%-H';
$percent_h = '%H' if (strftime('%-H', gmtime(0)) !~ /^\d+$/);
sub handler ($)
{
my ($q) = @_;
try_read_config($q->url());
my $path = $q->path_info();
$path =~ s/^\///;
$path =~ s/\/$//;
if (defined $directories{$path}) {
if ($q->path_info() =~ /\/$/) {
print_dir($path);
} else {
print "Location: ", $q->url(-path_info=>1), "/\n\n";
}
return;
}

my ($dir, $stat, $ext) = ($q->path_info() =~
/^(.*)\/([^\/]+)(\.html|-(day|week|month|year)\.$imagetype)$/);

$dir =~ s/^\///;

print_error("Undefined statistics")
unless defined $targets{$stat};

print_error("Incorrect directory")
unless defined $targets{$stat}{directory} || $targets{$stat}{directory} eq $dir;

my $tgt = $targets{$stat};

common_args($stat, $tgt, $q);

# We may be running under mod_perl or something. Do not destroy
# the original settings of timezone.
my $oldtz;
if (defined $tgt->{timezone}) {
$oldtz = $ENV{TZ};
$ENV{TZ} = $tgt->{timezone};
}

if ($ext eq '.html') {
do_html($tgt);
} elsif ($ext eq '-day.' . $imagetype) {
do_image($tgt, 'day');
} elsif ($ext eq '-week.' . $imagetype) {
do_image($tgt, 'week');
} elsif ($ext eq '-month.' . $imagetype) {
do_image($tgt, 'month');
} elsif ($ext eq '-year.' . $imagetype) {
do_image($tgt, 'year');
} else {
print_error("Unknown extension");
}
$ENV{TZ} = $oldtz
if defined $oldtz;
}

sub do_html($)
{
my ($tgt) = @_;

my @day = do_image($tgt, 'day');
my @week = do_image($tgt, 'week');
my @month = do_image($tgt, 'month');
my @year = do_image($tgt, 'year');

http_headers('text/html', $tgt->{config});
print <<'EOF';
<HTML>
<HEAD>
<TITLE>
EOF
print $tgt->{title} if defined $tgt->{title};
print "</TITLE>\n";

html_comments($tgt, 'd', @{$day[0]}) if $#day != -1;
html_comments($tgt, 'w', @{$week[0]}) if $#week != -1;
html_comments($tgt, 'm', @{$month[0]}) if $#month != -1;
html_comments($tgt, 'y', @{$year[0]}) if $#year != -1;

print $tgt->{addhead} if defined $tgt->{addhead};

print "</HEAD>\n", $tgt->{bodytag}, "\n";

print $tgt->{pagetop} if defined $tgt->{pagetop};

unless (defined $tgt->{options}{noinfo}) {
my @st = stat $tgt->{rrd};

print "

<hr>

\nThe statistics were last updated ",
strftime("<B>%A, %d %B, %H:%M:%S %Z</B>\n",
localtime($st[9]));
}

my $dayavg = $tgt->{config}->{interval};

html_graph($tgt, 'day', 'Daily', $dayavg . ' Minute', \@day);
html_graph($tgt, 'week', 'Weekly', '30 Minute', \@week);
html_graph($tgt, 'month', 'Monthly', '2 Hour', \@month);
html_graph($tgt, 'year', 'Yearly', '1 Day', \@year);

unless (defined $tgt->{options}{nolegend}) {
print <<EOF;

<hr>

<table WIDTH=500 BORDER=0 CELLPADDING=4 CELLSPACING=0>
EOF
print <<EOF unless ($tgt->{options}{noi});
<tr>
<td ALIGN=RIGHT><font SIZE=-1 COLOR="$tgt->{col1}">
<b>$tgt->{colname1} ###</b></font></td>
<td><font SIZE=-1>$tgt->{legend1}</font></td>
</tr>
EOF
print <<EOF unless ($tgt->{options}{noo});
<tr>
<td ALIGN=RIGHT><font SIZE=-1 COLOR="$tgt->{col2}">
<b>$tgt->{colname2} ###</b></font></td>
<td><font SIZE=-1>$tgt->{legend2}</font></td>
</tr>
EOF
if ($tgt->{withpeak} ne '') {
print <<EOF unless ($tgt->{options}{noi});
<tr>
<td ALIGN=RIGHT><font SIZE=-1 COLOR="$tgt->{col3}">
<b>$tgt->{colname3} ###</b></font></td>
<td><font SIZE=-1>$tgt->{legend3}</font></td>
</tr>
EOF
print <<EOF unless ($tgt->{options}{noo});
<tr>
<td ALIGN=RIGHT><font SIZE=-1 COLOR="$tgt->{col4}">
<b>$tgt->{colname4} ###</b></font></td>
<td><font SIZE=-1>$tgt->{legend4}</font></td>
</tr>
EOF
}
print <<EOF;</table>
EOF
}

print_banner($tgt->{config})
unless defined $tgt->{options}{nobanner};

print $tgt->{pagefoot} if defined $tgt->{pagefoot};
print "\n", <<'EOF';
</body>
</html>
EOF

}

sub html_comments($$@)
{
my ($tgt, $letter, @val) = @_;

return if $#val == -1;

unless ($tgt->{options}{noi}) {
print "<!-- maxin $letter ", $val[1], " -->\n";
print "<!-- avin $letter ", $val[3], " -->\n";
print "<!-- cuin $letter ", $val[5], " -->\n";
}
unless ($tgt->{options}{noo}) {
print "<!-- maxout $letter ", $val[0], " -->\n";
print "<!-- avout $letter ", $val[2], " -->\n";
print "<!-- cuout $letter ", $val[4], " -->\n";
}
}
sub html_graph($$$$$)
{
my ($tgt, $ext, $freq, $period, $params) = @_;

return unless defined $tgt->{$ext};

my @values = @{$params->[0]};
my $x = $params->[1];
my $y = $params->[2];

$x *= $tgt->{xzoom} if defined $tgt->{xzoom};
$y *= $tgt->{yzoom} if defined $tgt->{yzoom};

my $kilo = $tgt->{kilo};
my @kmg = split(',', $tgt->{kmg});

my $fmt;
if (defined $tgt->{options}{integer}) {
$fmt = '%d';
} else {
$fmt = '%.1f';
}

my @percent = do_percent($tgt, \@values);
my @relpercent = do_relpercent($tgt, \@values);

my @nv;
for my $val (@values) {
if (@kmg == 0) { # kMG[target]: <empty>
push @nv, sprintf($fmt, $val);
next;
}
for my $si (@kmg) {
if ($val < 10000) {
push @nv, sprintf($fmt, $val) . " $si";
last;
}
$val /= $kilo;
}
}
@values = @nv;

print "

<hr>

\n<B>\`$freq\' Graph ($period Average)</B>
\n";

print '<IMG SRC="', $tgt->{url}, '-', $ext, '.' . $imagetype .
'" WIDTH=', $x, ' HEIGHT=', $y, ' ALT="', $freq,
' Graph" VSPACE=10 ALIGN=TOP>
', "\n";
print '
<TABLE CELLPADDING=0 CELLSPACING=0>';
print <<EOF if $tgt->{legendi} ne '' && !$tgt->{options}{noi};
<TR>
<TD ALIGN=RIGHT><SMALL>Max <FONT COLOR="$tgt->{col1}">$tgt->{legendi}</FONT></SMALL></TD>
<TD ALIGN=RIGHT><SMALL>&nbsp;$values[1]$tgt->{shortlegend}$percent[1]</SMALL></TD>
<TD WIDTH=5></TD>
<TD ALIGN=RIGHT><SMALL>Average <FONT COLOR="$tgt->{col1}">$tgt->{legendi}</FONT></SMALL></TD>
<TD ALIGN=RIGHT><SMALL>&nbsp;$values[3]$tgt->{shortlegend}$percent[3]</SMALL></TD>
<TD WIDTH=5></TD>
<TD ALIGN=RIGHT><SMALL>Current <FONT COLOR="$tgt->{col1}">$tgt->{legendi}</FONT></SMALL></TD>
<TD ALIGN=RIGHT><SMALL>&nbsp;$values[5]$tgt->{shortlegend}$percent[5]</SMALL></TD>
</TR>

EOF
print <<EOF if $tgt->{legendo} ne '' && !$tgt->{options}{noo};
<TR>
<TD ALIGN=RIGHT><SMALL>Max <FONT COLOR="$tgt->{col2}">$tgt->{legendo}</FONT></SMALL></TD>
<TD ALIGN=RIGHT><SMALL>&nbsp;$values[0]$tgt->{shortlegend}$percent[0]</SMALL></TD>
<TD WIDTH=5></TD>
<TD ALIGN=RIGHT><SMALL>Average <FONT COLOR="$tgt->{col2}">$tgt->{legendo}</FONT></SMALL></TD>
<TD ALIGN=RIGHT><SMALL>&nbsp;$values[2]$tgt->{shortlegend}$percent[2]</SMALL></TD>
<TD WIDTH=5></TD>
<TD ALIGN=RIGHT><SMALL>Current <FONT COLOR="$tgt->{col2}">$tgt->{legendo}</FONT></SMALL></TD>
<TD ALIGN=RIGHT><SMALL>&nbsp;$values[4]$tgt->{shortlegend}$percent[4]</SMALL></TD>

EOF
if (defined $tgt->{options}{dorelpercent}) {
print <<"EOF";
</TR>
<TR>
<TD ALIGN=RIGHT><SMALL>Max <FONT COLOR="$tgt->{col5}">&nbsp;Percentage:</FONT></SMALL></TD>
<TD ALIGN=RIGHT><SMALL>&nbsp;$relpercent[0]</SMALL></TD>
<TD WIDTH=5></TD>
<TD ALIGN=RIGHT><SMALL>Average <FONT COLOR="$tgt->{col5}">&nbsp;Percentage:</FONT></SMALL></TD>
<TD ALIGN=RIGHT><SMALL>&nbsp;$relpercent[1]</SMALL></TD>
<TD WIDTH=5></TD>
<TD ALIGN=RIGHT><SMALL>Current <FONT COLOR="$tgt->{col5}">&nbsp;Percentage:</FONT></SMALL></TD>
<TD ALIGN=RIGHT><SMALL>&nbsp;$relpercent[2]</SMALL></TD>

EOF
}
print <<'EOF';
</TR>
</TABLE>

EOF
}

sub do_percent($$)
{
my ($tgt, $values) = @_;

my @percent = ('', '', '', '', '', '');

return @percent if defined $tgt->{options}{nopercent};

for my $val (0..$#$values) {
my $mx = ($val % 2 == 1) ? $tgt->{maxbytes1} : $tgt->{maxbytes2};
next unless defined $mx;
my $p = sprintf("%.1f", $values->[$val]*100/$mx);
$percent[$val] = ' (' . $p . '%)';
}

@percent;
}

sub do_relpercent($$)
{
my ($tgt, $values) = @_;

my @percent = ('', '', '');

return @percent unless defined $tgt->{options}{dorelpercent};

for my $val (0..2) {
$percent[$val] = sprintf("%.1f",
$values->[2*$val+1] * 100 / $values->[2*$val])
if $values->[2*$val] > 0;
$percent[$val] ||= 0;
$percent[$val] .= ' %';
}

@percent;
}

sub http_headers($$)
{
my ($content_type, $cfg) = @_;

print <<"EOF"; Content-Type: $content_type Refresh: $cfg->{refresh}
Pragma: no-cache
EOF
# Expires header calculation stolen from CGI.pm
print strftime("Expires: %a, %d %b %Y %H:%M:%S GMT\n",
gmtime(time+60*$cfg->{interval}));

print "\n";
}

sub do_image($$)
{
my ($target, $ext) = @_;

my $file = $target->{$ext};

return unless defined $file;

# Now the vertical rule at the end of the day
my @t = localtime(time);
$t[0] = $t[1] = $t[2] = 0;

my $seconds;
my $oldsec;
my $back;
my $xgrid;

my $unscaled;
my $withpeak;

my $noi = 1 if $target->{options}{noi};
my $noo = 1 if $target->{options}{noo};

if ($ext eq 'day') {
$seconds = timelocal(@t);
$back = 30*3600; # 30 hours
$oldsec = $seconds - 86400;
$unscaled = 1 if $target->{unscaled} =~ /d/;
$withpeak = 1 if $target->{withpeak} =~ /d/;
# We need this only for day graph. The other ones
# are magically correct.
$xgrid = 'HOUR:1:HOUR:6:HOUR:2:0:' . $percent_h;
} elsif ($ext eq 'week') {
$seconds = timelocal(@t);
$t[6] = ($t[6]+6) % 7;
$seconds -= $t[6]*86400;
$back = 8*86400; # 8 days
$oldsec = $seconds - 7*86400;
$unscaled = 1 if $target->{unscaled} =~ /w/;
$withpeak = 1 if $target->{withpeak} =~ /w/;
} elsif ($ext eq 'month') {
$t[3] = 1;
$seconds = timelocal(@t);
$back = 36*86400; # 36 days
$oldsec = $seconds - 30*86400; # FIXME (the right # of days!!)
$unscaled = 1 if $target->{unscaled} =~ /m/;
$withpeak = 1 if $target->{withpeak} =~ /m/;
} elsif ($ext eq 'year') {
$t[3] = 1;
$t[4] = 0;
$seconds = timelocal(@t);
$back = 396*86400; # 365 + 31 days
$oldsec = $seconds - 365*86400; # FIXME (the right # of days!!)
$unscaled = 1 if $target->{unscaled} =~ /y/;
$withpeak = 1 if $target->{withpeak} =~ /y/;
} else {
print_error("Unknown file extension: $ext");
}

my @local_args;

if ($unscaled) {
@local_args = ('-u', $target->{maxbytes1});
push @local_args, '--rigid' unless defined $target->{absmax};
}

if ($xgrid) {
push @local_args, '-x', $xgrid;
}

my @local_args_end;

if ($withpeak) {
push @local_args_end, 'LINE1:maxin'.$target->{col3}.':MaxIn'
unless $noi;
push @local_args_end, 'LINE1:maxout'.$target->{col4}.':MaxOut'
unless $noo;
}

my @rv = RRDs::graph($file, '-s', "-$back", @local_args,
@{$target->{args}}, "VRULE:$oldsec#ff0000",
"VRULE:$seconds#ff0000", @local_args_end);

my $rrd_error = RRDs::error;
print_error("RRDs::graph failed, $rrd_error") if defined $rrd_error;

# In array context just return the values
if (wantarray) {
if (defined $target->{factor}) {
@{$rv[0]} = map { $_ * $target->{factor} } @{$rv[0]};
}
if ($noi) {
return ([$rv[0][0], 0, $rv[0][1], 0, $rv[0][2], 0],
$rv[1], $rv[2]);
} elsif ($noo) {
return ([0, $rv[0][0], 0, $rv[0][1], 0, $rv[0][2]],
$rv[1], $rv[2]);
} else {
return @rv;
}
}

# Not in array context ==> print out the PNG file.
open PNG, "<$file" or print_error("Can't open $file: $!"); binmode PNG; http_headers("image/$imagetype", $target->{config});

my $buf;
# could be sendfile in Linux😉
while(sysread PNG, $buf, 8192) {
print $buf;
}
close PNG;
}

sub common_args($$$)
{
my ($name, $target, $q) = @_;

return @{$target->{args}} if defined @{$target->{args}};

my $noi = 1 if $target->{options}{noi};
my $noo = 1 if $target->{options}{noo};

$target->{name} = $name;

$target->{directory} = ''
unless defined $target->{directory};

my $tdir = $target->{directory};
$tdir .= '/'
unless $tdir eq '' || $tdir =~ /\/$/;

$target->{url} = $q->url . '/' . $tdir . $name;

my $cfg = $target->{config};

my $dir = $cfg->{workdir};
$dir = $cfg->{logdir}
if defined $cfg->{logdir};

$target->{rrd} = $dir . '/' . $tdir . $name . '.rrd';

%{$target->{options}} = ()
unless defined %{$target->{options}};

$dir = $cfg->{workdir};
$dir = $cfg->{imagedir}
if defined $cfg->{imagedir};

$target->{suppress} ||= '';

$target->{day} = $dir . '/' . $tdir . $name
. '-day.' . $imagetype unless $target->{suppress} =~ /d/;
$target->{week} = $dir . '/' . $tdir . $name
. '-week.' . $imagetype unless $target->{suppress} =~ /w/;
$target->{month} = $dir . '/' . $tdir . $name
. '-month.' . $imagetype unless $target->{suppress} =~ /m/;
$target->{year} = $dir . '/' . $tdir . $name
. '-year.' . $imagetype unless $target->{suppress} =~ /y/;

$target->{maxbytes1} = $target->{maxbytes}
if defined $target->{maxbytes} && !defined $target->{maxbytes1};

$target->{maxbytes2} = $target->{maxbytes1}
if defined $target->{maxbytes1} && !defined $target->{maxbytes2};

my @args = ();

push @args, '--lazy', '-c', 'FONT#000000', '-c',
'MGRID#000000', '-c', 'FRAME#000000',
'-g', '-l', '0';

$target->{background} = '#f5f5f5'
unless defined $target->{background};

push @args, '-c', 'BACK'. $target->{background};

push @args, '-c', 'SHADEA' . $target->{background},
'-c', 'SHADEB' . $target->{background}
if defined $target->{options}{noborder};

if (defined $target->{options}{noarrow}) {
push @args, '-c', 'ARROW' . $target->{background};
} else {
push @args, '-c', 'ARROW#000000';
}

push @args, '-b', $target->{kilo}
if defined $target->{kilo};

if (defined $target->{xsize}) {
if (defined $target->{xscale}) {
push @args, '-w', $target->{xsize}*$target->{xscale};
} else {
push @args, '-w', $target->{xsize};
}
}

if (defined $target->{ysize}) {
if (defined $target->{yscale}) {
push @args, '-h', $target->{ysize}*$target->{yscale};
} else {
push @args, '-h', $target->{ysize};
}
}

my $scale = 1;

if (defined $target->{options}->{perminute}) {
$scale *= 60;
} elsif (defined $target->{options}->{perhour}) {
$scale *= 3600;
}

if (defined $target->{options}->{bits}) {
$scale *= 8;
$target->{ylegend} = 'Bits per second'
unless defined $target->{ylegend};
$target->{legend1} = 'Incoming Traffic in Bits per Second'
unless defined $target->{legend1};
$target->{legend2} = 'Outgoing Traffic in Bits per Second'
unless defined $target->{legend2};
$target->{legend3} = 'Peak Incoming Traffic in Bits per Second'
unless defined $target->{legend3};
$target->{legend4} = 'Peak Outgoing Traffic in Bits per Second'
unless defined $target->{legend4};
$target->{shortlegend} = 'b/s'
unless defined $target->{shortlegend};
} else {
$target->{ylegend} = 'Bytes per second'
unless defined $target->{ylegend};
$target->{legend1} = 'Incoming Traffic in Bytes per Second'
unless defined $target->{legend1};
$target->{legend2} = 'Outgoing Traffic in Bytes per Second'
unless defined $target->{legend2};
$target->{legend3} = 'Peak Incoming Traffic in Bytes per Second'
unless defined $target->{legend3};
$target->{legend4} = 'Peak Outgoing Traffic in Bytes per Second'
unless defined $target->{legend4};
$target->{shortlegend} = 'B/s'
unless defined $target->{shortlegend};
}

if ($scale > 1) {
push @args, "DEF:in0=$target->{rrd}:ds0:AVERAGE",
"CDEF:in=in0,$scale,*",
"DEF:maxin0=$target->{rrd}:ds0:MAX",
"CDEF:maxin=maxin0,$scale,*"
unless $noi;
push @args, "DEF:out0=$target->{rrd}:ds1:AVERAGE",
"CDEF:out=out0,$scale,*",
"DEF:maxout0=$target->{rrd}:ds1:MAX",
"CDEF:maxout=maxout0,$scale,*"
unless $noo;
} else {
push @args, "DEF:in=$target->{rrd}:ds0:AVERAGE",
"DEF:maxin=$target->{rrd}:ds0:MAX"
unless $noi;
push @args, "DEF:out=$target->{rrd}:ds1:AVERAGE",
"DEF:maxout=$target->{rrd}:ds1:MAX"
unless $noo;
}

my $i=1;
for my $coltext (split(/,/, $target->{colours})) {
my ($text, $rgb) = ($coltext =~ /^([^#]+)(#[0-9a-fA-F]{6})$/);
$target->{'col'.$i} = $rgb;
$target->{'colname'.$i} = $text;
$i++;
}

push @args, '-v', $target->{ylegend};

push @args, 'AREA:in' . $target->{col1} . ':In',
unless $noi;
push @args, 'LINE2:out' . $target->{col2} . ':Out'
unless $noo;

push @args, 'PRINT:out:MAX:%.1lf' unless $noo;
push @args, 'PRINT:in:MAX:%.1lf' unless $noi;
push @args, 'PRINT:out:AVERAGE:%.1lf' unless $noo;
push @args, 'PRINT:in:AVERAGE:%.1lf' unless $noi;
push @args, 'PRINT:out:LAST:%.1lf' unless $noo;
push @args, 'PRINT:in:LAST:%.1lf' unless $noi;

if (defined $target->{maxbytes1}) {
$target->{maxbytes1} *= $scale;
push @args, 'HRULE:' . $target->{maxbytes1} . '#cc0000';
}

if (defined $target->{maxbytes2}) {
$target->{maxbytes2} *= $scale;
push @args, 'HRULE:' . $target->{maxbytes2} . '#cccc00'
if $target->{maxbytes2} != $target->{maxbytes1};
}

@{$target->{args}} = @args;

@args;
}

sub try_read_config($)
{
my ($prefix) = (@_);
$prefix =~ s/\/[^\/]*$//;

# Verify the version of RRDtool:
if (!defined $RRDs::VERSION || $RRDs::VERSION < 1.000331) {
print_error("Please install more up-to date RRDtool - need at least 1.000331");
}

my $read_cfg;
if (!defined $config_time) {
$read_cfg = 1;
} else {
for my $file (@all_config_files) {
my @stat = stat $file;
if ($config_time < $stat[9]) { $read_cfg = 1; last; } } } return unless $read_cfg; my %defaults = ( xsize => 400,
ysize => 100,
kmg => ',k,M,G,T,P',
kilo => 1000,
bodytag => "<BODY BGCOLOR=#ffffff>\n",
colours => 'GREEN#00cc00,BLUE#0000ff,DARK GREEN#006600,MAGENTA#ff00ff,AMBER#ef9f4f',
legendi => '&nbsp;In:',
legendo => '&nbsp;Out:',
unscaled => '',
withpeak => '',
directory => '',
);

%targets = ();

@all_config_files = @config_files;

my $order = 0;
for my $cfgfile (@config_files) {
%{$targets{_}} = %defaults;
%{$targets{'^'}} = ();
%{$targets{'$'}} = ();

my $cfgref = {
refresh => 300,
interval => 5,
icondir => $prefix
};

read_mrtg_config($cfgfile, \%defaults, $cfgref, \$order);
}

delete $targets{'^'};
delete $targets{_};
delete $targets{'$'};

parse_directories();

$config_time = time;
}

sub read_mrtg_config($$$$);

sub read_mrtg_config($$$$)
{
my ($file, $def, $cfgref, $order) = @_;

my %defaults = %$def;

my @lines;

open(CFG, "<$file") || print_error("Cannot open config file: $!");
while (<CFG>) {
chomp; # remove newline
s/\s+$//; # remove trailing space
s/\s+/ /g; # collapse white spaces to ' '
next if /^ *\#/; # skip comment lines
next if /^\s*$/; # skip empty lines
if (/^ \S/) { # multiline options
$lines[$#lines] .= $_;
} else {
push @lines, $_;
}
}
close CFG;

foreach (@lines) {
if (/^\s*([\w\d]+)\[(\S+)\]\s*:\s*(.*)$/) {
my ($tgt, $opt, $val) = (lc($2), lc($1), $3);
unless (exists $targets{$tgt}) {
# pre-set defaults constructed on all of ^, _, and $
for my $key (%{$targets{'^'}}) {
$targets{$tgt}{$key} = $targets{'^'}{$key};
}
for my $key (%{$targets{'$'}}) {
$targets{$tgt}{$key} .= $targets{'$'}{$key};
}
# WARNING: Tobi explicitly said that when all ^, _, and $
# options are set, the result should be just the value
# of the _ option (when the option itself is not explicitly
# defined. I do not agree with him here but I respect this
# and will try to be compatible with MRTG.
for my $key (%{$targets{'_'}}) {
$targets{$tgt}{$key} = $targets{'_'}{$key};
}

# anonymous hash ref - need copy, not ref
delete $targets{$tgt}{options};
# The same as above - we need to create this
# based on [^], [_], and [$] values
%{$targets{$tgt}{options}} = ();
%{$targets{$tgt}{options}} = %{$targets{'^'}{options}}
if defined $targets{'^'}{options};
%{$targets{$tgt}{options}} = (%{$targets{$tgt}{options}},
%{$targets{'_'}{options}})
if defined $targets{'_'}{options};
%{$targets{$tgt}{options}} = (%{$targets{$tgt}{options}},
%{$targets{'$'}{options}})
if defined $targets{'$'}{options};

$targets{$tgt}{order} = ++$$order;
$targets{$tgt}{config} = $cfgref;
}
if ($tgt eq '_' && $val eq '') {
if (defined $defaults{$opt}) {
$targets{_}{$opt} = $defaults{$opt};
} else {
delete $targets{_}{$opt};
}
} elsif (($tgt eq '^' || $tgt eq '$') && $val eq '') {
delete $targets{$tgt}{$opt};
} elsif ($opt eq 'options') {
# Do not forget defaults [^] and [$]
delete $targets{$tgt}{options};
%{$targets{$tgt}{options}} = %{$targets{'^'}{options}}
if defined $targets{'^'}{options};
$val = lc($val);
map { $targets{$tgt}{options}{$_} = 1 } ($val =~ m/([a-z]+)/g);
%{$targets{$tgt}{options}} = (%{$targets{$tgt}{options}},
%{$targets{'$'}{options}})
if defined $targets{'$'}{options};
} else {
my $pre = $targets{'^'}{$opt}
if defined $targets{'^'}{$opt};
$pre ||= '';
$targets{$tgt}{$opt} = $pre.$val;
$targets{$tgt}{$opt} .= $targets{'$'}{$opt}
if defined $targets{'$'}{$opt};
}
next;
} elsif (/^Include\s*:\s*(\S*)$/) {
push @all_config_files, $1;
read_mrtg_config($1, $def, $cfgref, $order);
next;
} elsif (/^([\w\d]+)\s*:\s*(\S.*)$/) {
my ($opt, $val) = (lc($1), $2);
$cfgref->{$opt} = $val;
next;
}
print_error("Parse error in $file near $_");
}

if (defined $cfgref->{pathadd}) {
$ENV{PATH} .= ':'.$cfgref->{pathadd};
}

# if (defined $cfgref->{libadd}) {
# use lib $cfgref->{libadd}
# }
}

sub parse_directories {
%directories = ();

# FIXME: the sort is expensive
for my $name (sort { $targets{$a}{order} <=> $targets{$b}{order} } keys %targets) {
my $dir = $targets{$name}{directory}
if defined $targets{$name}{directory};
$dir = '' unless defined $dir;

my $prefix = '';
for my $component (split /\/+/, $dir) {
unless (defined $directories{$prefix.$component}) {
push (@{$directories{$prefix}{subdir}},
$component);

# For the directory, get the global parameters
# from the # config of the first item of the
# directory:
$directories{$prefix}{config} =
$targets{$name}{config};
$directories{$prefix}{bodytag} =
$targets{$name}{bodytag};
}
$prefix .= $component . '/';
}
unless (defined $directories{$dir}) {
$directories{$dir}{config} =
$targets{$name}{config};
$directories{$dir}{bodytag} =
$targets{$name}{bodytag};
}

push (@{$directories{$dir}{target}}, $name);
}
}

sub print_dir($) {
my ($dir) = @_;

my $dir1 = $dir . '/';

http_headers('text/html', $directories{$dir}{config});

print <<EOF;
<HTML>
<HEAD>
<TITLE>MRTG: Directory $dir1</TITLE>
</HEAD>
EOF
print $directories{$dir}{bodytag};

my $subdirs_printed;
if (defined @{$directories{$dir}{subdir}}) {
$subdirs_printed = 1;
print <<EOF;
<H1>MRTG subdirectories in the directory $dir1</H1>
<UL>
EOF
for my $item (@{$directories{$dir}{subdir}}) {
print "
<LI><A HREF=\"$item/\">$item/</A>\n";
}

print "</UL>

\n";
}
if (defined @{$directories{$dir}{target}}) {
print "

<hr>

\n" if defined $subdirs_printed;
print <<EOF;
<H1>MRTG graphs in the directory $dir1</H1>
<TABLE BORDER=0 WIDTH=100%>
EOF
my $odd;
for my $item (@{$directories{$dir}{target}}) {
my $itemname = $item;
$itemname = $targets{$item}{title}
if defined $targets{$item}{title};
print "
<TR>\n" unless $odd;
print <<EOF;
<TD><A HREF="$item.html">$itemname
<IMG SRC="$item-day.$imagetype" BORDER=0 ALIGN=TOP VSPACE=10 ALT="$item">
</A><BR CLEAR=ALL>
</TD>

EOF
print " </TR>

\n" if $odd;
$odd = !$odd;
}
print " </TR>

\n</TABLE>

\n";
}

print_banner($directories{$dir}{config});
print "</BODY>\n</HTML>\n";
}

sub print_banner($) {
my $cfg = shift;

print <<EOF;

<hr>

<table BORDER=0 CELLSPACING=0 CELLPADDING=0>
<tr>
<td WIDTH=63><a ALT="MRTG" HREF="http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html"><img BORDER=0 SRC="$cfg->{icondir}/mrtg-l.$imagetype"></a></td>
<td WIDTH=25><a ALT="" HREF="http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html"><img BORDER=0 SRC="$cfg->{icondir}/mrtg-m.$imagetype"></a></td>
<td WIDTH=388><a ALT="" HREF="http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html"><img BORDER=0 SRC="$cfg->{icondir}/mrtg-r.$imagetype"></a></td>
</tr>
</table>
<spacer TYPE=VERTICAL SIZE=4>
<table BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=476>
<tr VALIGN=top>
<td ALIGN=LEFT><font FACE="Arial,Helvetica" SIZE=2>
version 2.9.17</font></td>
<td ALIGN=RIGHT><font FACE="Arial,Helvetica" SIZE=2>
<a HREF="http://ee-staff.ethz.ch/~oetiker/">Tobias Oetiker</a>
<a HREF="mailto:oetiker\@ee.ethz.ch">&lt;oetiker\@ee.ethz.ch&gt;</a>
</font></td>
</tr>
<tr>
<td></td>
<td ALIGN=RIGHT><font FACE="Arial,Helvetica" SIZE=2>
and&nbsp;<a HREF="http://www.bungi.com">Dave&nbsp;Rand</a>&nbsp;<a HREF="mailto:dlr\@bungi.com">&lt;dlr\@bungi.com&gt;</a></font></td>
<tr VALIGN=top>
<td ALIGN=LEFT><font FACE="Arial,Helvetica" SIZE=2>
<A HREF=http://www.fi.muni.cz/~kas/mrtg-rrd/>mrtg-rrd.cgi version $version</A>
</font></td>
<td ALIGN=RIGHT><font FACE="Arial,Helvetica" SIZE=2>
<A HREF="http://www.fi.muni.cz/~kas/">Jan "Yenya" Kasprzak</A>
<A HREF="mailto:kas\@fi.muni.cz">&lt;kas\@fi.muni.cz&gt;</A>
</font></td>
</tr>
</table>
EOF
print '<!--$Id: mrtg-rrd.cgi,v 1.35 2003/08/18 15:58:57 kas Exp $-->', "\n";
}

sub dump_targets() {
for my $tgt (keys %targets) {
print "Target $tgt:\n";
for my $opt (keys %{$targets{$tgt}}) {
if ($opt eq 'options') {
print "\toptions: ";
for my $o1 (keys %{$targets{$tgt}{options}}) {
print $o1, ",";
}
print "\n";
next;
}
print "\t$opt: ", $targets{$tgt}{$opt}, "\n";
}
}
}

sub dump_directories {
print "Directories:\n";

for my $dir (keys %directories) {
print "Directory $dir:\n";
for my $item (@{$directories{$dir}}) {
print "\t$item\n";
}
}
}

sub print_error(@)
{
print "Content-Type: text/plain\n\nError: ", join(' ', @_), "\n";
exit 0;
}

#--BEGIN CGI--
#For CGI, use this:

use CGI;
my $q = new CGI;

# thttpd fix up by Akihiro Sagawa
if ($q->server_software() =~ m|^thttpd/|) {
my $path = $q->path_info();
$path .= '/' if ($q->script_name=~ m|/$|);
$q->path_info($path);
}

handler($q);

#--END CGI--
#--BEGIN FCGI--
# For FastCGI, uncomment this and comment out the above:
#-# use FCGI;
#-# use CGI;
#-#
#-# my $req = FCGI::Request();
#-#
#-# while ($req->Accept >= 0) {
#-# my $q = new CGI;
#-#
#-# # thttpd fix up by Akihiro Sagawa
#-# if ($q->server_software() =~ m|^thttpd/|) {
#-# my $path = $q->path_info();
#-# $path .= '/' if ($q->script_name=~ m|/$|);
#-# $q->path_info($path);
#-# }
#-#
#-# handler($q);
#-# }
#--END FCGI--

1;


Few Tips

Make sure you install rrdtool before using it🙂

apt-get -y install rrdtool

To execute MRTG with command manually

env LANG=C mrtg /etc/mrtg.cfg

To assign apache www-data users full rights to /var/www/mrtg folder

chown -R www-data /var/www/mrtg/

SNMPWALK sample command to query any OID from snmp enabled target machine

snmpwalk -v1 -c gt 10.0.0.1  .1.3.6.1.2.1.25.2.3.1.6.65536

CFGMAKER command to create interface commands (this will support acquiring data larget then 120 M limit) as explained here. ( https://aacable.wordpress.com/tag/mrtg-120m-limit/ )

cfgmaker public@10.0.0.1:::::2 > temp.cfg

MRTG crontab sample (1 minute interval when using RRD)

*/1 * * * * env LANG=C mrtg /etc/mrtg.cfg –logging /var/log/mrtg.log

Enable CGI

sudo a2enmod cgi

Can’t locate RRDs.pm in @INC

SOLUTION:

sudo apt-get install librrds-perl


Some Sample Outputs !

WAN Usage: [Below …]

1-wan

Noc Room Temperature

1- noc room temp.PNG

Disk Space Used in Percent %

2- disk space of mail.PNG

Active PPP Users on CCR_1036

2-active-pppoe

CPU Usage of CCR_1036

3-mt-cpu-load

PING to Google DNS via CCR_1036

4-ping

Memory Usage of CCR_1036

5-memory

RB Temperature of CCR_1036

6-ccr-1036-rb-board-temperature

RB Voltage history of CCR_1036

7-rb-ccr-1036-voltage

SERVER UPTIME

3- server uptime.PNG

Memory Usage of Linux System

8-radius-memory


Regard’s
~ Syed Jahanza!B ~


Filed under: Linux Related, Mikrotik Related

SNMP on Centos missing few disk in snmp query

$
0
0

Image result for knowledge is power

If you have done a deep dive into the Ubuntu Ocean, and then start using Centos, you may realize that a quote “Ubuntu: made for Humans” is quite true indeed. z@ib

Recently I was configuring rrdtool/mrtg for remote Centos servers to to graphs disk storage , and found the difference between  MIb/OID of Windows/Ubuntu and Centos structure, therefore just sharing some notes for personnel references.

The problem I was getting in centos snmp querying that it was unable to see htStorageUsed.x counters. plus all mounted disks were not showing, only single disk was appearing in the for query result.

[for centos] To show all the disks , first edit the snmpd config file

nano /etc/snmp/snmpd.conf

add the following in end

disk /
includeAllDisks 10%

Restart the snmp service by

service snmpd restart

after a break, query the disk and you will find your missing disks🙂

(Showing relevant Data only)

#df -h
/dev/sdX1 829G 310G 519G 38% /X/folder1
/dev/sdX2 3.6T 69M 3.4T 1% /X/folder2
/dev/sdX3 1.9T 1.9T 4.7G 100% /X/folder3

#snmpwalk -v1 -c PUBLIC localhost .1.3.6.1.4.1.2021 |grep dskDevice
UCD-SNMP-MIB::dskDevice.8  = STRING: /dev/sdX1
UCD-SNMP-MIB::dskDevice.10 = STRING: /dev/sdX2
UCD-SNMP-MIB::dskDevice.11 = STRING: /dev/sdX3


# snmpwalk -v1 -c PUBLIC -m /cfg/mibs/UCD-SNMP-MIB 192.168.0.1 dskPercent
UCD-SNMP-MIB::dskPercent.8 = INTEGER: 96
UCD-SNMP-MIB::dskPercent.10 = INTEGER: 58
UCD-SNMP-MIB::dskPercent.11 = INTEGER: 12


CFG File Examples :

I used 2 different methods to to plot disk storage.


# 192.168.200.1 / 4 TB disk
Target[192.168.200.1_4tb]: ( dskUsed.10&dskUsed.10:public@192.168.200.1 ) * 100 / ( dskTotal.10&dskTotal.10:public@192.168.200.1 ) / 8
Title[192.168.200.1_4tb]: Disk usage for FTP Portal 192.168.200.1 / 4 TB Disk
PageTop[192.168.200.1_4tb]: <H1> Disk usage for FTP Portal 192.168.200.1 / 4 TB Disk</H1>
MaxBytes[192.168.200.1_4tb]: 100
AbsMax[192.168.200.1_4tb]: 100
ShortLegend[192.168.200.1_4tb]: %
YLegend[192.168.200.1_4tb]: % of DISK
Legend1[192.168.200.1_4tb]: Used DISK
LegendI[192.168.200.1_4tb]: Used :
LegendO[192.168.200.1_4tb]:
Options[192.168.200.1_4tb]: growright,gauge,integer,nobanner,printrouter,pngdate,nopercent
#Unscaled[192.168.200.1_4tb]: ymwd

# 192.168.200.1 / 2 TB disk - dskPercent.x
Target[192.168.200.1_2tb]: dskPercent.8&dskPercent.8:public@192.168.200.1 / 8
Title[192.168.200.1_2tb]: Disk usage for FTP Portal 192.168.200.1 / 2 TB Disk / FOLDER1
PageTop[192.168.200.1_2tb]: <H1> Disk usage for FTP Portal 192.168.200.1 / 2 TB Disk / FOLDER1</H1>
MaxBytes[192.168.200.1_2tb]: 100
AbsMax[192.168.200.1_2tb]: 100
ShortLegend[192.168.200.1_2tb]: %
YLegend[192.168.200.1_2tb]: % of DISK
Legend1[192.168.200.1_2tb]: Used DISK
LegendI[192.168.200.1_2tb]: Used :
LegendO[192.168.200.1_2tb]:
Options[192.168.200.1_2tb]: growright,gauge,integer,nobanner,printrouter,pngdate,nopercent
#Unscaled[192.168.200.1_2tb]: ymwd


Graphs Example:

ftp-disk-usage

combined


 


Filed under: Linux Related

Fighting with Ransomware !

$
0
0

ransomware

What is Ransomware 

Quite a HOT topic Now a days. Every email server administrator must be well familiarized with this malware and they try even harder to protect their users from this malware attack called locky (and many other similar variants) which encrypts users word/excel/etc documents and asks for money to restore them).

Our company users got hitted by this most smart malware repeatedly / several times resulting in great loss. Luckily most users data got recovered from Tape backup. This malware which usually comes by email , posted itself to be some valid / legitimate payment query & no matter how many times we provide users with education/warning about this matter, user stills open it considering it as valid email resulting in lost (encrypt/inaccessible) of all word/excel files.

We are using IBM Lotus Domino Email System along with Symantec Mail Security for Domino. With lots of R&D I am still unable to block this ransomware which comes in .JS files hidden inside .ZIP container. If I block .JS file inside the ZIP container, it will block legitimate PDF files as well. How frustrating ! The symantec should post some simple update to fix this issue. This issue is well discussed over here.

https://www.symantec.com/connect/ideas/exceptions-file-name-rule-smsmse


Workaround ! [Use Domain Group Policy to alter File Association Open with for .JS extension]

DISCLAIMER: THIS IS NOT A SOLUTION ! BUT JUST A `WORKAROUND` YOU CAN REFER TO.

THE PROPER SOLUTION WOULD BE TO USE SOME INTELLIGENT / UPDATED ANTISPAM/FILTRATION SYSTEM FOR YOUR EMAIL SYSTEM.

Since we cannot change our Symantec enterprise protection suite as it is covered under 3 years renewal (till 2018) . therefore aftering conducting lot of R&D I finally made a workaround for our DOMAIN USERS which is working Good so far.

I changed the .JS file extension OPEN WITH policy pointed to NOTEPAD.
[via Group_Policy ]

This way even if the user try to opens the .JS file, it will be open by notepad.
(instead of Windows Scripting Host)

I made changes to our domain controller in Windows 2008 R2

  • Login to Domain Controller PC
  • Open Group Policy (or by issuing following command)
    %SystemRoot%\system32\mmc.exe %SystemRoot%\system32\gpmc.msc
  • Edit the Default Domain Policy (or any custom you may have)
  • Goto User Configuration > Preferences > Control Panel Settings > Folder Options
  • Now Right click on Folder Options > New > Open With
  • Now use the below defined method as a reference to Update/Create the file extension

As shown in below image …

group policy.png

  • Action: Update
  • Files Extension: js
  • Assoticated Progra: %windir%\system32\notepad.exe
  • Set As Default : Select Tick on it

 

At client either issue gpupdate /force or restart the client pc or wait for the policy update.

Now try to open any .JS file and it will be opened in NOTEPAD (instead by windows scripting host program) and thus it will do no harm to user computer😀😀😀

Please test this method and let us know your feedback on it🙂

Note: you can use the same method to block / alter the file extension using Local Group POlicy


Alhamdolillah !

Regard’s
Syed Jahanzaib

 


Filed under: Microsoft Related, Symentec Related
Viewing all 408 articles
Browse latest View live