Showing posts with label perl. Show all posts
Showing posts with label perl. Show all posts

perl - generate high cpu / system load

Useful in testing monitoring scripts and performance tools.

#!/usr/bin/perl

use strict;
use Getopt::Long;
 
chomp(my $numcpu = `grep ^processor /proc/cpuinfo | wc -l`);

GetOptions (
    'c|cpu=i'  =>  \$numcpu    # defaults to the number of cpu on the system
);

while (--$numcpu and fork) {
}; 

while () {
}
source

copy-n-paste addition

[m@home bin]$ chmod +x add; cat add
#!/usr/bin/perl

use strict;
$|=1;

my $i;
while () {
    chomp;
    last if (/^END$/);
    $i += sprintf("%f", $_);
}
print "$i\n";

So I can just hold the 'ctrl' key to highlight numbers in columns:

[m@home bin]$ cat expenses.09.2012
rent:                   700
mom:                    900
household:              900
citi:                   1000                    (est 1000, 656.16 as of 8/21)
sc salary adv:          1500
sc balance transfer:    850
dbs loan:               4953                  
tax:                    254
fdw:                    265
pub:                    200
starhub_mae:            50
starhub(pro-rate):      50

[m@home bin]$ cat <<EOF | add
> 700
> 900
> 900
> 1000 
> 1500
> 850
> 4953 
> 254
> 265
> 200
> 50
> 50
> EOF
11622

watch command in Solaris

It doesn't come by default. There's a package in sunfreeware though, but if you don't have root access to install packages like I did, and if your needs are simple enough, this one should suffice:

#!/usr/bin/perl

use strict;
use Getopt::Long;

my %o;
GetOptions(
    "interval|n=i"    => \$o{n},
    "difference|d"    => \$o{d},
);

$o{n} |= 5;
my $command = join(' ', @ARGV);
while (1) {
    print `clear`;
    printf "%-30s %s\n\n",  "Every $o{n} s: $command", scalar(localtime());
    print `$command`, "\n";
    sleep $o{n};
}

I'll add some diff highlighting when I get some time

Solaris has no seq's

no problem!

[SunOS: marc@admin ~]$ cat `which seq`
#!/usr/bin/env perl

use strict;

sub Usage {
die "usage: $0 [lower-bound(optional)] [upper-bound]\n";
}

foreach (@ARGV) {
chomp;
if (!/\d+/) {
Usage();
}
}

my ($lower, $upper);
Usage() if $#ARGV > 1;
if ($#ARGV == 0) {
$lower = 1;
$upper = $ARGV[0];
} else {
$lower = $ARGV[0];
$upper = $ARGV[1];
}

while ($lower <= $upper) {
print $lower, "\n";
$lower++;
}

wrapping perl around bash

(or, wrapping _anything_ around bash)

Ignore the "nfs unmount" part, it's just a quick and dirty way to get the thing done. The neat trick here was the use of single quotes around 'EOF'. This way, I was able to embed perl code without having to worry about interpolation. No ugly escaping required ;)


#!/bin/bash

# quick and dirty
# assumes that anything that's not solaris is linux.

HOST=$1
cat <<'EOF' | ssh -q -oStrictHostKeyChecking=no -oConnectTimeOut=10 $HOST 'cat > /tmp/nfsUnmount.pl;
chmod +x /tmp/nfsUnmount.pl; /tmp/nfsUnmount.pl'
#!/usr/bin/perl

use strict;

die "execute me as root!\n" if $> ne 0;

# main
my @mounts = @{grabNsMounts()};
if (@mounts) {
foreach (@mounts) {
unmount($_);
}
} else {
print "no nfs mounts found!\n";
}
handleNscd();

exit 0;

sub handleNscd {
qx[pkill -9 nscd];
if ($^O eq "solaris") {
print qx[svcadm enable nscd];
} else {
print qx[/etc/init.d/nscd restart];
}
return 0;
}

sub unmount {
my $share = shift;
die "empty share detected" if ! $share;
chomp(my $out = qx[umount $share 2>&1]);
if ($?) {
print "WARNING: $share encountered some errors while unmounting: $out. forcing unmount...\n";
fUnmount($share);
} else {
if (qx[mount | grep $share | grep -v grep]) {
print "WARNING: $share is still mounted, force unmounting...\n";
fUnmount($share);
}
}
}

sub fUnmount {
my $share = shift;
die "empty share detected" if ! $share;
my $out;
if ($^O eq "solaris") {
chomp($out = qx[umount -f $share 2>&1]);
} else {
chomp($out = qx[umount -l $share 2>&1]);
}
if (qx[mount | grep $share | grep -v grep]) {
print "ERROR: $share is still mounted after force/lazy unmount. Skipping...\n";
}
return 0;
}

sub grabNfsMounts {
my ($out, @shares);
if ($^O eq "solaris") {
$out = qx[mount -p | grep ' nfs '];
} else {
$out = qx[mount -t nfs];
}
my @out = split(/\n/, $out);
foreach (@out) {
push(@shares, (split(/\s+/, $_))[2]);
}
return \@shares;
}
EOF

supporting different timezones

This nifty little script helps me schedule work involving people across different timezones:


[marc@home ~]$ cat bin/tzlist
#!/usr/bin/perl

use strict;
# TZ files(Solaris) /usr/share/lib/zoneinfo
# TZ files(Linux) '/usr/share/zoneinfo'
my $format = '%14s%-1s';
foreach (qw(
Singapore
America/New_York
America/Los_Angeles
Japan
Australia/Sydney
Europe/London
)) {
my $a = $_;
$a =~ s/^\w+\///;
my $arg;
if (@ARGV) {
$arg = "@ARGV";
} else {
$arg = 'now';
}
printf $format, "$a:\t", `TZ="$_" date -d "$arg"`;
}


output:

[marc@home ~]$ tzlist
Singapore: Fri Aug 5 22:56:03 SGT 2011
New_York: Fri Aug 5 10:56:03 EDT 2011
Los_Angeles: Fri Aug 5 07:56:03 PDT 2011
Japan: Fri Aug 5 23:56:03 JST 2011
Sydney: Sat Aug 6 00:56:03 EST 2011
London: Fri Aug 5 15:56:03 BST 2011

[marc@home ~]$ tzlist 8/13/2011 10:00 AM EDT
Singapore: Sat Aug 13 22:00:00 SGT 2011
New_York: Sat Aug 13 10:00:00 EDT 2011
Los_Angeles: Sat Aug 13 07:00:00 PDT 2011
Japan: Sat Aug 13 23:00:00 JST 2011
Sydney: Sun Aug 14 00:00:00 EST 2011
London: Sat Aug 13 15:00:00 BST 2011


Just add more timezone data if required

-m