Monday, May 2, 2011

Perl Script for Disk Management in Linux

Here is a script that detects any read write operation to your hard disk.
You can also change the location to any other as required by giving the path to $dir


#!/usr/bin/perl
use strict;
use warnings;
use Filesys::DiskSpace;

my $dir = "/";

my $old_free=194466108;

my $warning_level=10;
while (1)
{
my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir;
my $df_free = (($avail) / ($avail+$used)) * 100.0;


if ($df_free < $warning_level) {

if ($old_free == $avail)
{
#do nothing
}
else
{
my $now = localtime time;
my $new_av = ($avail / 1000 );
my $out = sprintf("Read/Write from disk Detected at $now. Free Disk space is %0.2f%% ($new_av MB) WARNING:LOW DISK SPACE!!! PLEASE REMOVE SOME ITEMS\n",$df_free);
print $out;
$old_free = $avail;
}
}
else
{
if ($old_free == $avail)
{
#do nothing
}
else
{
my $now = localtime time;
my $new_av = ($avail / 1000 );
my $out = sprintf("Read/Write from disk Detected at $now. Free Disk space is %0.2f%% ($new_av MB) YOU CAN STILL COPY ITEMS TO THIS DISK\n",$df_free);
print $out;
$old_free = $avail;
}
}
}

Perl script that converts a log file to a Excel sheet

This script detects "|" and separates data. You can edit this code to enable for both whitespace and "|" or only whitespace.

#!/usr/bin/perl -w

use strict;
use Spreadsheet::WriteExcel;


if($#ARGV ne 1)
{
print "\n Usage: txt2xls \n Example: txt2xls \"|\" *.txt\n\n";
}

my $token;
my $file;
my $del;
my $wb;
my @files = @ARGV[1..$#ARGV];

foreach $file (@files){
open (TXTFILE, "$file") or die;
my $wb = Spreadsheet::WriteExcel->new("$file.xls");
my $excel = $wb->addworksheet();
my $row = 0;
my $col;

while () {
chomp;

if ($ARGV[0] =~ /\|/)
{
$del="\\|";
}
else
{
$del = $ARGV[0];
}

my @Fld = split(/$del/, $_);

$col = 0;
foreach $token (@Fld) {
$excel->write($row, $col, $token);
$col++;
}
$row++;
}
}


Shell script to send files to another computer

This is a simple shell script that can send a set of files to a particular PC. Also next time it sends, it will auto-create a backup file of previous sent files with data and time .

This script might be useful for those who want to read a log file.



filename=bacup_`date +%b_%d_%Y_%H_%M_%S`

ssh username@ipaddress_of_desti << EOT

mkdir /home/username/bin

cp /home/username/bin/ls
/home/username/bin/$filename

EOT
scp /bin/ls username@:
ipaddress_of_desti/home/username/bin/


exit