Wednesday, October 29, 2008

Using AT to schedule a command for later in Linux / CentOS

I know this should be one of those basic things ALL Systems Administrators knows but I just learned it today.  I just re-installed a  server with CentOS 5.2.  While it is mostly up to date there are a bunch of packages that need to be updated.  I didn't want to hog all of our bandwidth during production hours and I forget to do stuff once I get home, so I want this to happen with out me thinking about it too much.

From a command prompt I typed:
at midnight tomorrow [ENTER]
yum -y update [ENTER]
[CTLR + D]
 This set up my command to update via my Yum repositories tonight at 12:00:00am (well technically tomorrow).

That is all.

Tuesday, October 28, 2008

Using nagios to check PHP script execution time

We want to record the execution time of some of our PHP scripts.  I want to use nagios to do this but it is proving rather difficult.

Initially I was hoping I could provide some credentials to nagios for it to use and log into our web site, run a set of scripts, and then report back to us how long each script took.  Since our site does not use basic authentication I cannot do it the intial way I wanted.  

To work around the issues we are having and hopefully NOT have to write our own nagios plug-in here is what I am proposing.

Create a test directory that uses basic authentication so it is protected from the general public and has a set of scripts that we run with the execution time posted near the bottom.  We then use the standard check_http plug-in from nagios to search for that string and parse it as normal. 

Hopefully this approach to things won't take too long to develop and prototype.  Also I hope to report back with a follow up on what I actually did (with code examples) to get this working.

Monday, October 27, 2008

Adding a calender to your BASH login script

You milage may vary, but on CentOS 5.2 I edited my .bash_profile to contain the line:
cal `date +"%m"` `date +"%Y"`
This put a nice looking calendar on my screen when I logged in vi ssh.  A small addition but handy non the less.

Thursday, October 23, 2008

Nagios check_mysql_connections example

I had a little bit of trouble getting the check_mysql_connections working but once I figured it out on the command line I had to get it working in nagios.  Still being a nagios noob I had a little trouble but also got it working in nagios.

The line below will work from the command line.
[user@host libexec]# ./check_mysql_connections -w 75 -c 90 -H 555.555.555.555 -u YOUR_USER -p YOUR_PASS

In the host file I setup the service as 

define service{
        host_name                       db1.mrswi.com
        service_description             MySQL Connections
        check_command                   check_mysql_connections! -w 75 -c 90 -H 555.555.555.555 -u YOUR_USER -p YOUR_PASS
        check_period                    24x7
        max_check_attempts              2
        notification_interval           30
        notification_options            c,w
        contact_groups                  mrsitis
        }




Powershell: Equal Sign vs. -eq

I was just working on a script in powershell and saw the error "Length" is a ReadOnly property. It was from the code if ($args.count = 0) {exit;}.  Having "grown up" writing VB code this would have worked.  But in powershell you need to do if ($args.count -eq 0) {exit;} instead.

Powershell: Accept Unknown number of Args and process them

Recently I needed a script to move some files for a user.  It might be two or three files but I wanted to exersise my noggin in powershell.  This code will take any number of arguments and copy the files from the input directory to the output directory. Below is the code.

::Start Code::
# Display Help
if (($Args[0] -eq "-?") -or ($Args[0] -eq "-help")) {
   ""
   "Usage: pullQualityAssesments.ps1 "
   "            copies the Faxjob over to the Quality Assesments folder."
   ""
   "Example: pullQualityAssesments.ps1 400001 400002"
   ""
   exit
}
$inputdir = "\\someserver\savedfaxjobs\"
$outputdir = "\\someotherserver\Quality Assessments\"
$x = $args.count
$i = 0
do {
   $ordernumber = $args[$i]
   #write-host $inputdir$ordernumber.faxjob.* $outputdir
   $filelist = get-childitem $inputdir$ordernumber.faxjob.*
   foreach ($file in $filelist){write-host $file}
   $i++}
while ($i -le $x)
::End Code::

Unfortunatly we have 23,000+ files in our Input directory and this script is slower than me pulling them manually.  Maybe someday I can revisit this to filter it down to something that works and pulls in only the files it needs.

Powershell Check MySQL Status

The script below will check the specified server using the credentials provided and report back to you all of the Status Variables. You will have to change the line "$myconnection.ConnectionString" to what ever your settings are.  On a side note drop the <> from the lines.  Also you will need the MySQL .Net Connector for this to work.


::Start Code::
[void][system.reflection.Assembly]::LoadFrom("C:\Program Files\MySQL\MySQL Connector Net 5.2.3\Binaries\.NET 2.0\MySQL.Data.dll")

$myconnection = New-Object MySql.Data.MySqlClient.MySqlConnection
$myconnection.ConnectionString = "server=;user id=;password=;pooling=false"
$myconnection.Open()

$mycommand = New-Object MySql.Data.MySqlClient.MySqlCommand
$mycommand.Connection = $myconnection
$mycommand.CommandText = "SHOW STATUS;"
$myreader = $mycommand.ExecuteReader()
while($myreader.Read()){write-host $myreader.getstring(0)"|"$myreader.getstring(1)}
$mycommand.Connection.close()
::End Code::

If you are only interested in a two or three of those variables (say threads_running and threads_connected) you can use the code below.

[void][system.reflection.Assembly]::LoadFrom("C:\Program Files\MySQL\MySQL Connector Net 5.2.3\Binaries\.NET 2.0\MySQL.Data.dll")

$myconnection = New-Object MySql.Data.MySqlClient.MySqlConnection
$myconnection.ConnectionString = "server=IP OR HOSTNAME;user id=YOUR_USER;password=YOUR_PASSWORD;pooling=false"
$myconnection.Open()

$mycommand = New-Object MySql.Data.MySqlClient.MySqlCommand
$mycommand.Connection = $myconnection
$mycommand.CommandText = "SHOW STATUS LIKE 'threads_connected';"
$myreader = $mycommand.ExecuteReader()

while($myreader.Read()){write-host $myreader.getstring(0)" : "$myreader.getstring(1)}
$mycommand.Connection.close()
$mycommand.connection.open()
$mycommand.CommandText = "SHOW STATUS LIKE 'threads_running';"
$myreader = $mycommand.executereader()
while($myreader.read()){write-host $myreader.getstring(0)" : "$myreader.getstring(1)}
$mycommand.Connection.close()