Tag: PERL

PERL language

Simple LDAP Caching Unix Daemon

Simple LDAP Caching Unix Daemon

Introduction-

I wrote this simple LDAP Caching unix daemon 2 yrs back when we faced lot of issue with Integrating Apache with LDAP authentication
using Apache ldap auth module. We were able to configure it properly however we faced slowness issue.

Basically We wanted to use Nagios (Check_mk Multisite) with LDAP authentication. So we wrote this unix daemon.
As of now this is very simple (no theading/forking and it is blocking) However it is working perfectly without any issue (for Nagios web interface authentication and few other web based tools, around 300+ users).

Download Perl files-

* ldapcached.pl
http://www.indiangnu.org/wp-content/uploads/2013/ldapcached-pl.txt

* ldapcached-client.pl
http://www.indiangnu.org/wp-content/uploads/2013/ldapcached-client-pl.txt

* Custom Apache Handler – to use this daemon for basic authentication
http://www.indiangnu.org/wp-content/uploads/2013/MyHandler-pm.txt

root@arunb:~# cat /etc/init.d/ldapcached-initd.pl
#!/usr/bin/perl
use strict;
use warnings;
use Proc::Daemon;

no warnings ‘uninitialized’;
if ( $ARGV[0] =~ m/start/ ) {
Proc::Daemon::Init;
my $continue = 1;
$SIG{TERM} = sub { $continue = 0 };
while ($continue) { eval { `/usr/local/ldapcached.pl –daemon`;};}

} else { print ” * Usage: $0 {start}”;}
#end
print “\n”;
root@arunb:~#

Step 1] Run Daemon – (edit LDAP related varibles)

Copy ldapcached.pl perl file at following location on your system /usr/local/ldapcached.pl
OR Change path in “ldapcached-initd.pl” startup file

* Start process-

root@:~# /etc/init.d/ldapcached-initd.pl start
root@:~#

* Make sure only one ldapcached process running?

root@:~# ps aux | grep ldap | grep -v grep
root 19441 0.0 0.0 6212 1408 ? S 15:03 0:00 /usr/bin/perl /etc/init.d/ldapcached-initd.pl start
root 19442 0.0 0.2 8656 4916 ? S 15:03 0:00 ldapcached
root@:~#

Step 2] How to test-

root@:~$ perl ldapcached-client.pl –client ‘my-ldap-user’ ‘my-ldap-pass’
Failed
root@:~$

root@:~$ perl ldapcached-client.pl –client ‘my-ldap-user’ ‘my-ldap-pass’
Pass
root@:~$

Step 3] How I should use/integrate in Application-

Say I want to use this ldapcached unix daemon for Apache/Basic Authentication –

NOTE- Make sure to copy MyHandler.pm module in Perl module directory or Check Apache error log for any error.

ScriptAlias /nagios/cgi-bin “/usr/lib64/nagios/cgi”

<Directory “/usr/lib64/nagios/cgi”>
Order allow,deny
Allow from all
AuthType Basic
AuthName “Nagios GUI”
PerlAuthenHandler Apache::MyHandler
Require valid-user
</Directory>

Thank you,
Arun

perl script to check Bind dns zone files

perl script to check Bind dns zone files

Introduction –

dns-zone-verify.pl is perl script used to check dns zone file syntax! This script verifies both Forward and Reverse Zone.
Internally perl script is using named-checkzone and named-checkconf. Bind dns commands to check zone file syntax.
Script is reading named.conf file to get all zone

* Download – Please download perl script from following locations…

http://www.indiangnu.org/wp-content/uploads/2012/dns-zone-verify-pl.txt

* How to run/use –

root@localhost~# perl ./dns-zone-verify.pl

* Usage: ./dns-zone-verify.pl { –verify }

root@localhost~# perl ./dns-zone-verify.pl –verify

zone myzone_internal.file/IN: loaded serial 2007013101
OK

zone myzone_external.file/IN: loaded serial 2012100527
OK
….
root@localhost~#

Thank you,
Arun Bagul

Nagios daily and weekly Reporting and log parsing

Nagios daily and weekly Reporting and log parsing

Introduction – We wrote perl script for Nagios daily and weekly Reporting. This script is parsing nagios.log for all alerts notification and generating nagios report in CSV format…

** How to use?

arunb@arunb:~$ perl /home/arunb/nagios-log-parsing.pl
* Usage: /home/arunb/nagios-log-parsing.pl { <nagios_log file>  [ –summary ] }
arunb@arunb:~$

** To Generate Daily Nagios Report –
arunb@arunb:~$ perl /home/arunb/nagios-log-parsing.pl  /usr/local/nagios/var/nagios.log
arunb@arunb:~$ perl /home/arunb/nagios-log-parsing.pl  /usr/local/nagios/var/nagios.log  –summary

arunb@arunb:~$ perl /home/arunb/nagios-log-parsing.pl  /usr/local/nagios/var/nagios.log  –summary  > /tmp/NAGIOS-daily.csv

** To Generate Weekly Nagios Report –

Add all weeks nagios.log files absolute path (per line) in file say “/tmp/nagios-logfile”

arunb@arunb:~$  for i in `cat /tmp/nagios-logfile`; do  perl /home/arunb/nagios-log-parsing.pl  $i; done | grep -v ^$ > /tmp/NAGIOS-weekly.csv

* Want to see the script ?

arunb@arunb:~$ cat /home/arunb/nagios-log-parsing.pl

#!/usr/bin/perl

my $file = $ARGV[0];
my $stime = 0;
my $htime = 0;
my ($s_alert,$h_alert ) = (undef,undef);

my $STATE_OK;
my $STATE_WARNING;
my $STATE_CRITICAL;
my $STATE_UNKNOWN;
my %STATE_COUNTER;
my %ALERT;

############################

if ($ARGV[0]) {
#my $file = "/usr/local/nagios/var/nagios.log";

open (MYFILE, "<$file" ) or die "Can;t open nagios file";
while(<MYFILE>) {
chomp;
my $line = $_;
##print "\n".$line;
#if ($line =~ m/[(.*)].*;(.*);(.*);(.*);.*/) {
#if ($line =~ m/\[(.*)\]\s+(HOST|SERVICE)\sNOTIFICATION:\s\w+;(.*);.*/) {
if ($line =~ m/\[(.*)\]\s+SERVICE\sNOTIFICATION:\s\w+;(.*);.*/) {
my $time = $1;
my $mydate = scalar(localtime($time));
my ($host,$service,$alert)  = split(';',$2);
#print "\nARUN=>$1 = $host,$service,$alert";
if ($s_alert eq $service) {
my $time_diff = $time - $stime;
if ($time_diff > 600 ) {
if ($alert ne "OK") {
if (exists $ALERT{"$service"}) { $ALERT{"$service"} = $ALERT{"$service"} + 1;}
else { $ALERT{"$service"} = 1 ; }
$STATE_COUNTER{"$alert"} = $STATE_COUNTER{"$alert"} + 1;
print "\n$mydate [$time],$host,$service,$alert";
}
}
} else {
if ($alert ne "OK") {
if (exists $ALERT{"$service"}) { $ALERT{"$service"} = $ALERT{"$service"} + 1;}
else { $ALERT{"$service"} = 1 ; }
$STATE_COUNTER{"$alert"} = $STATE_COUNTER{"$alert"} + 1;
print "\n$mydate [$time],$host,$service,$alert";
}
}
$stime = $time;
$s_alert = $service;

} elsif ($line =~ m/\[(.*)\]\s+HOST\sNOTIFICATION:\s\w+;(.*);.*/) {
my $time = $1;
my $mydate = scalar(localtime($time));
my ($host,$hdown,$alert)  = split(';',$2);
#print "\nARUN=>$1 = $host,$hdown";
if ($hdown eq "DOWN") {
if ($h_alert eq $host) {
my $time_diff = $time - $htime;
if ($time_diff > 600 ) {
$STATE_COUNTER{"$hdown"} = $STATE_COUNTER{"$hdown"} + 1;
print "\n$mydate [$time],$host,$hdown";
}
} else {
$STATE_COUNTER{"$hdown"} = $STATE_COUNTER{"$hdown"} + 1;
print "\n$mydate [$time],$host,$hdown";
}
}
$htime = $time;
$h_alert = $host;
}
}
## summary
my $total_counter = 0;
my $total_service = keys(%ALERT);
if ($ARGV[1] eq "--summary") {
print "\n\n"."-" x 20;
print "\nSummary Report\n";
print "-" x 20 . "\n";
print "\nAlert_Type,Total_Count";
while (my ($key,$value)=  each(%STATE_COUNTER)) {  print "\n$key,$value"; }
print "\n\nService_Name,Total_Count";
while (my ($key,$value)=  each(%ALERT)) { print "\n$key,$value"; $total_counter = $total_counter + $value; }
print "\n\nTotal Alerts = $total_counter";
print "\nTotal Service Failed = $total_service";
}

} else { print " * Usage: $0 { <nagios_log file>  [ --summary ] }";}

#end
print "\n";

** To download the script “http://www.indiangnu.org/wp-content/uploads/2011/nagios-log-parsing-pl.txt

Thank You,
Arun Bagul

Nagios log file with normal date/time format

Nagios log file with normal date/time format

Introduction –

Nagios log file print the time in epoch time format. To convert that nagios log with normal time/date format you can use the following perl script.

Please download the script from here- http://www.indiangnu.org/wp-content/uploads/2010/nagios-log-pl.txt

* How to use it –

root@me:~# cat /etc/nagios/var/nagios.log | grep -i  NOTIFICATION | /root/nagmon/nagios-log.pl

* Pasting the code here…

root@me:~#  /root/nagmon/nagios-log.pl
#!/usr/bin/perl

# Author - IndianGNU.org
# Read nagios.log file and show log with
# converted date field in human-readable format
# 

$numArgs = $#ARGV + 1;

if ( $ARGV[0] eq "-h")
{
	print " * Usage: cat   like '/usr/local/nagios/var/nagios.log' | $0 \n"; exit 1;
}

sub epochtime
{
  my $epoch_time = shift;
  ($sec,$min,$hour,$day,$month,$year) = localtime($epoch_time);

  # correct the date and month for humans
  $year = 1900 + $year;
  $month++;

  return sprintf("%02d/%02d/%02d %02d:%02d:%02d", $year, $month, $day, $hour, $min, $sec);
}

while (<>)
{
  my $epoch = substr $_, 1, 10;
  my $remainder = substr $_, 13;
  my $human_date = &epochtime($epoch);
  printf("[%s] %s", $human_date, $remainder);
}
exit;

Thanks you,
Arun