" />

Moving a directory from one host to another using tar over ssh

November 18th, 2009 mrochford No comments

tar cf - * | ssh "(cd && tar xf -)"

Categories: One Liner Tags:

Naigos Plugin: Check for packages to update in Debian/Ubuntu

November 5th, 2009 mrochford No comments

Over the years I have manages a lot of systems, mostly debian based. The thing I really hate about having a lot of standalone systems is updating software. I am really bad at keeping track of new updates and I really dont want to have a cron script running to send me a email every day. Below is a script I used as a plugin for nagios.

The Script: check_for_updates.pl

Categories: Linux, Nagios, Perl, Programming Tags:

Backup of your MYSQL server using mysqldump

October 22nd, 2009 mrochford No comments

Over the years of being a admin I have seen many ideas on how to backup a mysql database server. Many people are satisfied with with one large dump of all databases into one flat text file. Others think copy the actual db files to other locations are the way to back things up. I have found dealing with customer and internal databases you need incremental backups. With all that said I developed a perl script that will log in grab all existing database names and do a mysqldump of each database. It will also keep a specified amount of backups and you also can choose to have some logs gzip’ed or left uncompressed. Below is the link to the code. You will have update login credentials and the ip address of the mysql server.

Link: mysql_backup.pl

Explanation of the script:

The script will login to your mysql server and issue a “show databases;”. Once it the database names are retrieved it will run a mysqldump on each database and store the output in the specified directory. The name scheme of the out will be database_name-YYYYMMDD.sql. Doing this it will allow you to sort based on date. Once all mysqldumps have completed it will go through each database directory and audit what should be saved, deleted, gzipped or leave alone. You will be able to chose what the numbers of day you want to retain and the how many days you want to leave as a uncompressed file.

Categories: Mysql, Perl, Programming Tags:

Changing the I/O timeout on your SCSI disk

July 23rd, 2009 mrochford No comments

Changing your I/O timeout for your SCSI disks are rare. When you are running VMWare that houses the guest OS on a NFS/SAN mount you need to change the timeout. If you dont you will run into a mess with your guest OS. You will try running a snapshot and browsing your file system and bam its locks up. Below is what you need to change in your udev rules to change it.

Location: /etc/udev/rules.d/50-udev-default.rules


SUBSYSTEM=="scsi" , SYSFS{type}=="0|7|14", RUN+="/bin/sh -c 'echo 60 > /sys/block/sda/device/timeout'"
SUBSYSTEM=="scsi" , SYSFS{type}=="0|7|14", RUN+="/bin/sh -c 'echo 190 > /sys/block/sda/device/timeout'"
SUBSYSTEM=="scsi" , SYSFS{type}=="1", RUN+="/bin/sh -c 'echo 900 > /sys/block/sda/device/timeout'"

Categories: Linux Tags:

Connecting to a machine using sockets

May 27th, 2009 mrochford No comments

I work with a api system that connects on a high port. My co-work Matt came up with a great way to set the socket up and verify if the socket isn’t stale. This sub-routine is specific to the api but you can use the theory and apply it to pretty much your socket needs.

sub cp_connect {
     ###############
     #
     # This function connects to a cp server
     #
     # Accepts:
     # $remote_host The server to connect to
     # $remote_port The port to connect to
     #
     # Returns:
     # A critical Path mail server socket connection.
     #
     ###############

     my $remote_host = shift;
     my $remote_port = shift;
     my $remote_password = shift;
     my $remote_conneciton_type = shift;
     my $EOL = shift;

     if($remote_conneciton_type =~ m/^rw$/){
          $remote_password = $remote_password ." write";
     }
     
     my $socket = IO::Socket::INET->new(
          PeerAddr => $remote_host,
          PeerPort        => $remote_port,
          Proto           => "tcp",
          Type            => SOCK_STREAM,
          Timeout         => 5 )
          || die "Couldn’t open socket!\n\n";

     my $answer = <$socket>;
     print $socket "LOGIN $remote_password" . $EOL;
     $answer = <$socket>;
     if ($answer !~ /^OK/) {
          print "Failed to login to $remote_host : $remote_port : $answer\n\n";
          exit(1);
     }
     return $socket;
}
 

Once you have the understanding of how the sub-routine works. Building a script that interacts with the api is a piece of cake.

Here is an example of how we use the sub-routine in a script. This example will check to see if the socket is defined, if not it will try to connect and define the socket. Once the socket is defined it will issue a command and read the socket.

if(!defined $socket) {
    $socket = cp_connect($remote_host, $remote_port, $remote_rwpass,"rw",$EOL);
}
print $socket "DOMAIN ENUMERATE". $EOL;
while (defined (chomp($answer = <$socket>))){
    if($answer =~ /\*\s+(.+)\s/) {
        $domains{$1}{‘users’} = ();
    }
    elsif($answer =~ /^ERROR/){
        $error = $answer;
        return 0;
        last;
    }
    elsif($answer =~ /OK/) {
        return 1;
        last;
    }
}
 
Categories: Perl, Programming Tags:

sub-routine: gettime();

May 27th, 2009 mrochford No comments

Over the years working as a systems administrator I have a large sub-routine collection on my belt. One that I use quite frequently is gettime();. This is a simple routine someone in my group wrote to return a nicely formatted timestamp. It is use mostly for logging and such.

sub gettime () {
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    $year += 1900;
    $mon += 1;
    my $retval = sprintf("$year\-%02d\-%02d %02d:%02d:%02d",$mon,$mday,$hour,$min,$sec);
    return $retval;
}
 

Example:

#!/usr/bin/perl
print gettime(),"\n";
 

returns:

2009-05-27 07:18:43
 
Categories: Perl, Programming Tags:

Hiding your STDIN

May 26th, 2009 mrochford No comments

Ever write a script that you have to enter a elevated users password? or even your own? Check this out.

#!/usr/bin/perl
use Term::ReadKey;
print "Please enter your username: ";
chomp(my $username = <STDIN>);
print "Please enter your password for $username: ";
ReadMode(‘noecho’);
chomp(my $password = ReadLine(0));
ReadMode(‘normal’);

#for example only. You shouldn’t print this…
print "\nYou entered $username for a username and $password for a password!!\n";
 

Using Term:ReadKey you can disable echo for STDOUT. This way no one will see your password from over your shoulder.

Categories: Perl, Programming Tags:

Accessing gallery2 using the API to return information about a gallery

May 26th, 2009 mrochford No comments

I designed a website for a friend to sell a litter of puppies. He wanted a simple site that had galleries for each dog and one for the litter. I didn’t want to maintain the site at all so I used a lot of pre built software. I chose to use Gallery2 to do all the picture stuff. It did everything I wanted (resize,rotate,etc). I didn’t like how it looked though. I wanted to just have it do the back end stuff and I wanted to design the layout. To do this all I needed was the urls to access the images. Below is a function I wrote that will output all the information of a picture and its uri.

The script is located here

which outputs:

Array
(
    [0] => Array
        (
            [title] => IMG_0028.JPG
            [width] => 2272
            [height] => 1704
            [large_picture] => /p/d/71-1/IMG_0028.JPG
            [thumb_picture] => /p/d/72-2/IMG_0028.JPG
            [resized_picture] => Array
                (
                    [0] => /p/d/73-2/IMG_0028.JPG
                    [1] => /p/d/74-2/IMG_0028.JPG
                    [2] => /p/d/75-2/IMG_0028.JPG
                )

        )

    [1] => Array
        (
            [title] => IMG_0029.JPG
            [width] => 2272
            [height] => 1704
            [large_picture] => /p/d/77-1/IMG_0029.JPG
            [thumb_picture] => /p/d/78-2/IMG_0029.JPG
            [resized_picture] => Array
                (
                    [0] => /p/d/79-2/IMG_0029.JPG
                    [1] => /p/d/80-2/IMG_0029.JPG
                    [2] => /p/d/81-2/IMG_0029.JPG
                )

        )
)

You will need to modify the paths in the function to your installation.

A side note… To find gallery ids you will need to to run the following query.

mysql> SELECT g2_id,g2_title FROM g2_Item WHERE g2_canContainChildren=1;
Categories: PHP, Programming Tags:

Howto access an array that is in a hash

May 26th, 2009 mrochford No comments

Say you have a hash element that contains an array and you want to loop through the array set. You usually have these types of data sets when you are trying to gather data.

example:
We will use the following code to build the hash:

#!/usr/bin/perl
use Data::Dumper;
my %data_set;
$data_set{'users'} = ();
$data_set{'users'}[0] = "testA";
$data_set{'users'}[1] = "testB";
$data_set{'users'}[2] = "testC";
$data_set{'users'}[3] = "testD";
$data_set{'users'}[4] = "testE";
$data_set{'users'}[5] = "testF";
print Dumper(%data_set);

Output:

$VAR1 = 'users';
$VAR2 = [
'testA',
'testB',
'testC',
'testD'
];

Now that we know the hash element is populated we want to access each array element. This can be done with making some crazy counter in a while loop. Check it out.

foreach my $user (@{$data_set{'users'}}){
print $user,"\n";
}

Now knowing you can access a array with in a hash using @{} you can push,pop,splice and shift on the array that is in a hash.

Here is the same script above but using push:

#!/usr/bin/perl
use Data::Dumper;
my %data_set;
$data_set{'users'} = ();
push(@{$data_set{'users'}},"testA");
push(@{$data_set{'users'}},"testB");
push(@{$data_set{'users'}},"testC");
push(@{$data_set{'users'}},"testD");

foreach my $user (@{$data_set{'users'}}){
print $user,"\n";
}

Categories: Perl, Programming Tags:

Howto push a hash onto an array

May 26th, 2009 mrochford 1 comment

This information was giving to me by my friend Matt
use Data::Dumper;
my @array;
push @array, {‘key1′ => ‘value1′, ‘key2′ => ‘value2′};
push @array, {‘key1′ => ‘value1′, ‘key2′ => ‘value2′};
push @array, {‘key1′ => ‘value1′, ‘key2′ => ‘value2′};
print Dumper(@array);

Will give you:

$VAR1 = {
‘key2′ => ‘value2′,
‘key1′ => ‘value1′
};
$VAR2 = {
‘key2′ => ‘value2′,
‘key1′ => ‘value1′
};
$VAR3 = {
‘key2′ => ‘value2′,
‘key1′ => ‘value1′
};

Categories: Perl, Programming Tags: