Wednesday, 7 September 2011

Monitoring Citrix Xenapp licenses through Nagios

Hi all,

More scripts. Again in VBS. This time for monitoring citrix licensing.

All this script does is check to see how many licenses are being used as a percentage of the total. Then returns normal, warning or critical depending on %age.



' change this line to suit the location of the lmstat utility
' ***
CommandLine = "C:\Program Files\Citrix\Licensing\LS\lmstat -c ""C:\Program Files\Citrix\Licensing\MyFiles\license_file.lic"" -f MPS_ADV_CCU"
' ***

Set objShell = CreateObject("WScript.Shell")
Set oExec = objShell.Exec(CommandLine)
countLicenses = 0

Do Until oExec.StdOut.AtEndOfStream
mystring= oExec.StdOut.ReadLine
if InStr(mystring, "Users of MPS_ADV_CCU:") Then

issued_start = InStr(mystring,"(Total of ")
issued_len = InStr(mystring, " licenses issued;") - (issued_start + 10)

lic_total = Mid(mystring, issued_start + 10, issued_len)

'-------------------------------------------------------------------------------------------------------------------------------------------------
inuse_start = InStr(mystring,"; Total of ")
inuse_len = InStr(mystring, " licenses in use)") - (inuse_start + 12)


lic_inuse = Mid(mystring, inuse_start + 12, inuse_len)


pc = int(lic_inuse / lic_total * 100)
'pc = 93

if pc => 90 then


WScript.Echo "Critical - " & pc & "% in use"
WScript.Quit(2)
end if


if pc => 80 then


WScript.Echo "Warning - " & pc & "% in use"
WScript.Quit(1)
else

WScript.Echo "OK - " & pc & "% in use"
WScript.Quit(0)
end if


Exit Do

end if
Loop
There you have it. Normal restrictions apply. If you run it and it breaks your stuff, its your fault for running untested code. It works for me.

Import this script into nsclient++ and nagios in the normal way. I will document this at some point.

Thanks for reading.

Trev

VMWare VCB backups

Hi all,

When backing up many virtual machines, you may try one of the big backup solutions, such as Symantec, Commvault, Veeam, and the like. Unfortunately in this case they all rely on one thing.

THE VMWARE API

There is a problem with the API. VMWare are aware there is a problem with it (calls were logged late last year about the problem) but so far there has been no response at all.

The problem is that when a backup of a VM happens, it takes a snapshot of the machine.
The machines base disk and other chuff is backed up, then the snapshot is released. What is happening (to many people) is that the snapshot is removed from the snapshot manager, but the snapshot file is not deleted, and the deltas are still written to the snapshot.

Luckily(!) for me, my luns filled up and bombed out after 2 snapshots. There are reports out there that if you have 25 of these "ghost snapshots" then you start running into problems. For example redo log errors. The fix is easy enough, although a pain in the arse. Basically just run the convertor against it, and itll roll all the snaps together. Don't faff with the command line unless you really have to.

Anyhew, there is one option. In version 4.1 VCB still works. So you can use that. I have written a script you can use, usual terms apply. By usual terms I mean, if this fucks your environment its not my fault. Lose data, it's not my fault.

Here is the code:

Dim fso, ts, weight
weight=0
Const ForWriting = 2
set ofso2 = createobject("scripting.filesystemobject")
set ofiletemp = ofso2.opentextfile ("servers.txt", 1)
set cline = createobject("wscript.shell")
'----------------------------------------------------------------------
do while not ofiletemp.atendofstream
weight = weight + 1
servername = ofiletemp.readline
if weight < 6 then
'msgbox servername
fullpath = "vcbmounter -h [yourVCname] -u username -p Password -a name:" & servername & " -r e:\vmbackups\" & servername & " -t fullvm -m nbd"
cline.run fullpath, 1, False 'lets another copy open "NBD!!"
else
fullpath = "vcbmounter -h [yourVCname] -u username -p Password -a name:" & servername & " -r e:\vmbackups\" & servername & " -t fullvm -m nbd"
cline.run fullpath, 1, True 'waits for this one to finish before carrying on
weight = 0 'resets weight to 0
end if
loop

What you need to do is put this file in the VCB directory in c:\program files\vmware\vmware consolidated backup/

Create a servers.txt file that has a list of your servers in, one per line.

If you look in the code there is a file path to e:. That is the destination directory. Change it to a destination you like.

Once this script has finished, you will have a directory in your detination dir, for every machine, filled with the files that make up that machine. Back these machines up however you want, or just have them as a handy copy.

When you are finished, run this script:

Dim fso, ts, weight
weight=1
Const ForWriting = 2
set ofso2 = createobject("scripting.filesystemobject")
set ofiletemp = ofso2.opentextfile ("servers.txt", 1)
set cline = createobject("wscript.shell")
'----------------------------------------------------------------------
do while not ofiletemp.atendofstream
servername = ofiletemp.readline
'msgbox servername
fullpath = "vcbmounter -h [yourVCname] -u userid -p password -U e:\vmbackups\" & servername
'msgbox fullpath
cline.run fullpath, 1
loop

This will remove all those machine directories. Again, don't forget to change the file path beginning "e:\" in the script. This also needs to live in the VCB directory mentioned above.

This works without creating ghost snapshots. It won't work at all against ESX5.0 as far as I know.

Thanks for reading,

Trev

Friday, 10 June 2011

MSSQL - Finding running queries

Hi all,

I've just written this SQL for MS SQL 2005. It returns running sql queries.


select CN.session_id, ST.text, ST.dbid, ES.last_request_start_time,
ES.last_request_end_time, ES.login_name, ES.status
from sys.dm_exec_connections CN
join sys.dm_exec_sessions ES ON CN.session_id = ES.session_id
CROSS APPLY sys.dm_exec_sql_text(CN.most_recent_sql_handle) as ST
where ES.status = 'running'


Trev

Sunday, 5 June 2011

DHCP DDNS and BIND part deux

Hi all,

Configuring DHCP on Ubuntu 10.04.

This is a part 2, but can equally be used as a part 1. Install dhcp3 using apt-get.

The first thing you'll see is that after you install it it'll fail to start. That is deliberate. Leave it down.

First thing to do is to tell DHCP what ethernet port to use.

nano /etc/defaults/dhcp3-server

under the interfaces line, in the speech marks, put your interface. For example "eth1"

Save and close that. That's the easy bit.

Now we need to setup the bits and pieces.

nano /etc/dhcp3/dhcpd.conf

Either edit this one, or back it up. Anyway, the lines you want are these:

ddns-update-style interim;
ignore client-updates;


and these (I've left the comment line in so you can see where in the file I did this):

# A slightly different configuration for an internal subnet.
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.70 192.168.1.150;
option domain-name-servers [your DNS server NAME];
option domain-name "[your domain name]";
option routers 192.168.1.254;
option broadcast-address 192.168.1.255;
default-lease-time 600;
max-lease-time 7200;
}

include "/etc/bind/rndc.key";

zone 1.168.192.in-addr.arpa. {
primary [your DNS server IP];
key "rndc-key";
}
zone newburytechinfo.co.uk. {
primary [your DNS server IP];
key "rndc-key";
}


These are fairly easy to read. The last 2 bits on the zone. This is to automatically update your DNS server with your clients IPs and names. The include line, includes the key from BIND.

Change the IP ranges and whatnot for your network, turn off your current DHCP server, start this one.

sudo service dhcp3 start

On your PC type this:

sudo dhclient

You should get an IP preconfigured with the right gateway, and your dns server as first. You should also be able to ping it from another box.

That's it.

Oh. Some messing about might be required.

Do these lines if you start having errors:

cd /etc/apparmor.d
nano usr.sbin.named

add this line:

/etc/bind/zones/** rw,


save and close.

nano usr.sbin.dhcpd3

add these lines (no its not pretty, this is a hack):

/etc/bind/ rw,
/etc/bind/** rw,


Save and close.

Run this:

sudo service apparmor restart

That's it. It should all work.

Thanks for reading

Trev

DHCP DDNS and BIND

Hi all,

I've just installed BIND and DHCP onto my server as my router wasn't dealing very well with either role.

Here's how I did it on, yes, Ubuntu 10.04.

First, install BIND and DHCP3 (You can either sudo or switch to root).

apt-get install bind9 && apt-get install dhcp3-server

Lets sort out bind9 first.

nano /etc/bind/named.conf

Make a quick note of the include statements. As you can see these files of config are added to keep things simple. Anyway, on the end of this file add these lines:

controls {
inet 127.0.0.1 allow {localhost; } keys { "rndc-key";};
};


This is for later so DHCP can update DNS. Save it and lets do the next one, it's one on the include list.

nano /etc/bind/named.conf.options

Stick your forwarders in here. Cunningly where it says forwarders. Here is my setting with my ISPs 2 DNS servers in:

forwarders {
87.194.255.154;
87.194.255.155;
};


Save it, lets do the next one. Again its in the includes list from earlier.

nano /etc/bind/named.conf.local

Bit more difficult this one. You need to create some zones. Mine isn't visible from the internet so bugger it, you can have mine.

zone "newburytechinfo.co.uk"{
type master;
file "/etc/bind/zones/newburytechinfo.co.uk.db";
allow-update { key "rndc-key"; };
//allow-update { localhost; localnets; };
notify yes;
};


See what I've done? All you have to do is replace my domain name, with your internal domain name. // = comment. This is while I was debugging. If you don't care what on your network can update your DNS use that line instead of the key line.

Next up, reverse DNS. Stay where you are and create a new zone like this(again this is mine):

zone "1.168.192.in-addr.arpa"{
type master;
file "/etc/bind/zones/rev.1.168.192.in-addr.arpa";
allow-update { key "rndc-key"; };
notify yes;
};


Ok? Nice and simple this. For the zone, if your computers IP is (for example) 192.168.1.15, chop the last number and dot off, then reverse it. Then add ".in-addr.arpa" on the end. If your computers IP is 10.242.192.24, it'll end up looking like this: "192.242.10.in-addr.arpa".

Obviously change the "file" line up there too to match the zone. Last bit, stick this line on the end:

include "/etc/bind/rndc.key";


Save it.

Ok, next bit. Create a zones directory:

mkdir /etc/bind/zones

go in it

cd /etc/bind/zones

You know the 2 filenames referenced above in that script I gave you?

create a new file with your domain name, like this:

nano newburytechinfo.co.uk.db

You'll notice this file is referenced above in the zone config. Now we need to put the A records in. These I will clean. Cut and paste this. Change example.com to your domain name and the IP for the A record on the last line.

$ORIGIN .
$TTL 38400 ; 10 hours 40 minutes
example.com IN SOA example.com. admin.example.com. (
2007031009 ; serial
28800 ; refresh (8 hours)
3600 ; retry (1 hour)
604800 ; expire (1 week)
38400 ; minimum (10 hours 40 minutes)
)
NS example.com.
MX 10 example.com.
$ORIGIN example.com.
mail A 192.168.1.1

Right. Oh, by the by, that last line? If you want to give another server a fixed DNS name, chuck it in, in the same way. Save and exit.

Bounce the bind9 service:

service bind9 restart

Want to do a quick test? On your PC type this:

nano /etc/resolv.conf

delete everything (or put it somewhere else, write it down, back up your file, I'm not your mum), and put this in:

nameserver [your DNS server IP]


save it, now try and go to a website.

Have a coffee, you've just done your first DNS server.

Tell you what, I'll do DHCP as a seperate article!

Thanks for reading

Trev

Thursday, 2 June 2011

Kill all my processes! muhahaha!

Hi all,

This is an interesting command line a friend of mine wrote, I'm posting it here because its handy for killing database processes that are spinning and you can't sort it out nicely:

ps -ef |grep [someprocesseswiththesamename]|grep -Po "^\w+\s+\d+"| grep -Po "\d+"| sed "s/^/|kill -9 /" |sh

Say you have 50 apache2 services and apache is playing up so you want to kill all of them. (An example only...). You would do this:

ps -ef| grep apache2 |grep -Po "^\w+\s+\d+"|grep -Po "\d+"|sed "s/^/kill -9 /"|sh

Quick description:

ps -ef : List processes

grep apache2: Chops down the ps -ef to only show lines with apache2 in

grep -Po "^\w+\s+\d+" : Display just the name and PID

grep -Po "\d+" :Display just the PID

sed "s/^/kill -9 /" :build the command line, replaces the beginning of line char with "kill -9" the PID from the previous line gets appended.

sh: Pipe all that lot to the shell

Thanks for reading,

Trev

apt-get: Hash Sum mismatch

Hi All,

So, sometimes, just when you think that apt-cacher-ng is your amazing solution, or you have managed to get apt-get going through a proxy, you get hit by something like this:

Failed to fetch http://gb.archive.ubuntu.com/ubuntu/dists/lucid-updates/main/source/Sources.bz2 Hash Sum mismatch

That's no good is it? You try wget and that works fine, but apt just will not work. Here's how to fix it. Unfortunately I don't know what causes it. For me it has worked for ages and just broke one day and refused to work. Try the following (run as root):

touch /etc/apt/apt.conf.d/no-cache
nano /etc/apt/apt.conf.d/no-cache

type this line in and save it:

Acquire::http {No-Cache=True;};

The run:

apt-get update

This should now work with no problems.

Thanks for reading.

Trev