welcome: please sign in
location: zabbix / ZabbixFaq / SmsScriptExample

SMS Script as an example. You need to modify !!

with help by benny, DaTa and gray:

use strict;
use DBI;
use Date::Manip;
use LWP::UserAgent;
use HTTP::Request::Common;

# define connector
my $ua = new LWP::UserAgent(agent => 'AgentName/0.1');
$ua->no_proxy('www.somesmscarrier.tld');

# some variables
my $text = "@ARGV";
my $url = "http://www.somesmscarrier.tld/smsc.php";;
my $user = "smscarrieruse";
my $pass = "smscarrierpass";
my $from = "MONITORING";
my $date_cnf = "/home/www/zabbix/bin/Ino.cnf";
my $db = "zabbix";
my $dbhost = "carbon.dmz.somedomain.de";
my $dbuser = "zabbix";
my $dbpass = "zabbixdbpass";

# init our Date::Manip config
&Date_Init("GlobalCnf=$date_cnf");

# truncate $text to 140 chars
$text = substr($text, 0, 140);

# set database
my($dbh);
$dbh = &set_database($db, $dbhost, $dbuser, $dbpass);

my $dbq = $dbh->prepare("SELECT sendto FROM media, users WHERE users.alias LIKE
'ADM%' AND media.userid=users.userid AND media.active='0' AND
users.alias!='Admin'");
$dbq->execute;
my $numbers = $dbq->fetchall_arrayref;

# trigger send
if(!Date_IsWorkDay("now", 1)) {
sendsms();
}

# post wird hier gebaut
sub sendsms {
shift;
my %content;
$content{'user'} = $user;
$content{'pass'} = $pass;
$content{'from'} = $from;
$content{'text'} = $text;

foreach (@{ $numbers }) {
my $to = $_->[0];
next if($to !~ /^\d+$/);
$content{'to'} = $to;

my $resp = $ua->request( POST $url , Content_Type => 'form-data',
Content => [ %content ] );

printf("%s\n", $resp->error_as_HTML) unless $resp->is_success;

printf("%s\n", $resp->content());
}
}

sub set_database() {
my($db,$dbhost,$dbuser,$dbpass) = @_;
my($config);
my($dbstring) = "DBI:mysql:$db:host=$dbhost;";
my($dbh) = DBI->connect($dbstring, $dbuser, $dbpass);
if (!ref($dbh)) {
printf("db error: %s\n", $DBI::errstr);
exit;
}
return($dbh);
}

this is the conffile wich defines holidays and the working hours were sms notifications are not needed

################################
# CONFIG VARIABLES
################################
Language = English
DateFormat = US
TZ = CET
ConvTZ = CET
Internal = 0
FirstDay = 1
WorkWeekBeg = 1
WorkWeekEnd = 5
WorkDayBeg = 09:00
WorkDayEnd = 18:00
WorkDay24Hr = 0
TomorrowFirst = 1
DeltaSigns = 0
Jan1Week1 = 0
YYtoYYYY = 89
UpdateCurrTZ = 0
IntCharSet = 0

################################
# HOLIDAYS
################################
*HOLIDAY

1/1/2004 =
1/6/2004 =
2/23/2004 =
2/24/2004 =
2/25/2004 =
3/9/2004 =
3/11/2004 =
3/12/2004 =
5/1/2004 =
5/20/2004 =
5/30/2004 =
5/31/2004 =
6/10/2004 =
8/15/2004 =
10/3/2004 =
11/1/2004 =
11/14/2004 =
11/17/2004 =
11/21/2204 =
12/24/2204 =
12/25/2004 =
12/26/2004 =

some notes.. uncommented from source

By: Alexei Vladishev - hugetoad RE: sms script example 2004-05-25 15:49 I'm not sure why do connection to Zabbix DB. All necessary data (like recipient, subject, message) is passed directly to a media script as comand line parameters.

Regards, Alexei

By: Nobody/Anonymous - nobody RE: sms script example 2004-05-25 16:57 iam doing it because i aint like to define all triggers actions for any single user again. users come and users leave.. so it takes ages to hold all triggers uptodate. it safes lots of time by defining all actions for only the admin user with different severities and then to query the db for the ADM(Like)user with an active media type and sending alerts there. i hope you see my point..

( well i may should define triggers for groups but when i started with zabbix, group weren't implemented.. )

Regards, Spaceelk

By: Nobody/Anonymous - nobody and one more post... 2004-05-25 17:02 ... more specific explanation.. my workmates and me rotating hotline.. so only the 'hotline ADMuser of the weeks' mediatype containing the mobilenumber is activated at the start of the week. all other ADMlike users mediatypes getting disabled then. all erros are send to Admin and get parsed to smsscript. the script then decides where and if the message is send by sms...

zabbix/ZabbixFaq/SmsScriptExample (last edited 2008-07-14 09:55:41 by localhost)