Pages

Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

Friday, August 20, 2010

Simple script to alert you when websites are down

Suppose your clients and boss would like to know when a site you maintain should go down.

You can write your own script to achieve this goal, where you implement a light-weight http client in python-twisted... but why bother?

There is a thing called reinventing the wheel.


So what do we need?
  1. curl;
  2. logrotate;
  3. mutt;
  4. A list of urls;
  5. A log file to record visits by curl;
  6. A script called site-checker, which one puts in the /etc/cron.hourly/ and chmod 770 /etc/cron.hourly/site-checker:


    #!/bin/bash
    
    #set -e -x
    
    FILELOC=/home/user/bin/websites.list
    LOGFILE=/home/user/bin/log/site-checker.log
    WEBSITES=$(cat $FILELOC | xargs)
    UDATE=$(date -u)
    EMAILADDR=user@nowhere.com
    for w in $WEBSITES
    do
      echo "==Fetched $w on $UDATE==" >> $LOGFILE
      curl -sf $w || echo "Warning: $w is down on $UDATE" | mutt -s "[website down] $UDATE $w" $EMAILADDR $
    done
    

websites.list would contain something like this:

www.microsoft.com
www.google.com
www.sap.com
www.oracle.com

For mutt to send e-mails you either need to setup a MTA (mail-transfer agent) or add a .muttrc file to your $HOME, containing something like this:

set smtp_url="smtp://user@smtp.woohoo.com"
set smtp_pass="tuff2guess"

You also have to prevent your site-checker.log from growing too big with logrotate.

Type this in your bash shell:

echo -e "/home/user/bin/log/*.log {\n\
  daily\n\
  missingok\n\
  size=100k\n\
  rotate 5\n\
}" > /etc/logrotate.d/user-custom\


As with every single script on this blog, you must adapt the script to your own needs.

Happy hacking!


Update 21-dec-2010:
#!/bin/bash
FILELOC=/home/user/bin/websites.list
PEOPLELOC=/home/user/bin/people.list
LOGFILE=/home/user/bin/site-checker.log
WEBSITES=$(cat $FILELOC | xargs)
UDATE=$(date -u)

mail () {
  read message
  domainname=$1
  for addr in $(cat $PEOPLELOC | xargs)
  do
    echo $message | mutt $addr -s "[website down] $UDATE $domainname" 2> /dev/null
  done
}

for w in $WEBSITES
do
  touch $LOGFILE
  echo "==Fetched $w on $UDATE==" >> $LOGFILE
  curl -sf $w || echo "Warning: $w is down on $UDATE" | mail $w
done

Friday, August 13, 2010

File destroyer / obliterator

I stole the idea here and I wrote a a practical script to embellish on the theme.

#!/bin/sh

#set -e -x
set -e

if [ -f $1 ]; then
  echo "Are you sure? Hit ctrl+c to abort."
  for i in `seq 5`; do
    echo $i && sleep 1;
  done
  # mac osx / bsd: BYTES=$(stat -f%z $1)
  BYTES=$(/usr/bin/du -b $1 | sed 's/\([0-9]\+\).*/\1/')
  /bin/dd if=/dev/urandom of=$1 bs=$BYTES count=1 conv=notrunc
  echo "$1 has been written over with random bits."
  /bin/rm -f $1
  echo "$1 has been removed from the filesystem."
fi

Warning! Achtung baby! Do not use this on sensitive files you may need in the future.

This got me thinking. Imagine you work for a secretive branch of some government and you have sensitive info on your drive you wouldn't want to be caught with dead, like passwords etc. You could build a kill switch for your portable computer(s) with the sensitive files. This kill switch is attached to an artificial pacemaker which senses if you're still alive or not, by checking your heart-beat. This portable computer gets signals from the pacemaker. When it ceases to receive signals, the kill switch is engaged, after 5 minutes with no keep-alive signals from the pacemaker, the portable computer is erased completely.

The trick is to not let the signal get intercepted. Hmmmm... I should write spy-novels with a technical flavour.

Monday, May 24, 2010

Slackware 13.0 x86_64 cross-compiling to 32-bit

I'm a programmer.

I like programming.

I want my computer with slackware 13.0 x86_64 to churn out 32-bit and 64-bit linux binaries; and I want to run wine.

I want to learn to compile to multiple targets using HaXe. Those targets are flash, android, php, javascript, C/C++/SDL (iPhone). So, download it; unpack it (tar xf); install it as root.

So the first thing I had to do was to refit my x86_64 compiler to cross-compile to 32-bit.

The first thing you find on the intarweb is this: http://connie.slackware.com/~alien/multilib/. Don't skim it. You'll thank me. Oh, you also owe a thanks to Eric Hameleers (aka AlienBob) and Fred Emmott (aka fred, of slamd64 fame). Go on irc.freenode.net and thank them. Thanks to them you can run wine on slackware 13.0 x86_64 and thus access win32 applications e.g. Flash Develop (note: must get .NET 2 to work on wine; used winetricks but failed horribly, removed some fonts, gave up, subject of another blogpost ).

Do everything described in the previous link.

When compiling with slackbuilds from slackbuild.org (SBo), you need for 32-bit applicationsto run on your machine. It would be wise to run the slackbuild like this from the shell as root:


$ ARCH=i686 sh gc.SlackBuild


ARCH=i686 will force the variable inside the slackbuild script to take on this value: "i686" which activates a few parameters for gcc to compile to this architecture, which is the canonical x86 architecture. By the way, my example "gc" is Boehm's garbage collector. It is required for neko to run properly.

Next thing, learn to use "ldd". Type `which ldd` to find your ldd binary, it checks whether the dependencies of a certain binary you're running can find the libraries they depend on.

You might find out that the slackbuild for gc on SBo fails, with the following message:


checking for C compiler default output file name...
configure: error: C compiler cannot create executables
See `config.log' for more details.


Fortunately, someone only forgot to put "-m32" in this line:

...
elif [ "$ARCH" = "i686" ]; then
SLKCFLAGS="-O2 -march=i686 -mtune=i686" # <-- this line!


So add it there.

SLKCFLAGS="-O2 -march=i686 -mtune=i686 -m32"


Run it again, then you're good to go!

Good luck.