Useful Code

Comments Off

Here’s a collection of scripts that I’ve created and use in my capacity as a Systems Engineer. Hopefully, they may be useful to someone else out there.

Render simulator A shell script to simulate renders
genip Python script to generate zones and ips on a windows DNS server

  • Render simulator This is a shell script used to simulate renders. It’s great for testing schedular performance. It has options for adjusting memory, running local disk i/o, and can stagger the start and stop time. If you want it to also stress the cpu, uncomment the md5sum.
#!/bin/sh

# source function library
. /etc/rc.d/init.d/functions

proc="memtester"

THREADS=1
MEMTOTAL=`free | grep "^Mem" | awk '{print $2}'`
MEM=1920
SEC=600
BONNIE=0
RETVAL=0
RAN=0
TFRAME=20

do_usage()
{
    echo "usage $0: -t [threads] -m [memory] -s [seconds] -r [seconds] -b"
    echo -e "\t-t: [optional]  set number of processes to run (default 1) "
    echo -e "\t-m: [optional]  set memory per thread (default 1920)"
    echo -e "\t-b: [optional]  run bonnie on localdisk"
    echo -e "\t-r: [optional]  randomize finish time by number of seconds"
}

set_sleep() {

  echo $[ ( $RANDOM % $DIE_SIDES )  + 1 ]

}

while [ $# -gt 0 ]
do
   case $1
   in
      -t)
         THREADS=$2
         shift 2
      ;;
      -m)
         MEM=$2
         shift 2
      ;;
      -s)
         SEC=$2
         shift 2
      ;;
      -b)
         BONNIE=1
         shift
      ;;
      -h)
         do_usage
         exit 1
      ;;
      -r)
         RAN=1
         TFRAME=$2
         shift 2
      ;;
   esac
done

echo "$THREADS memtesters using $MEM per test"

for ((i=1;i<=$THREADS;i+=1)); do
    #md5sum < /dev/urandom &
    PID_NAME="sanity-$$-${i}"
    PID_FILE="/var/run/${PID_NAME}.pid"
    memtester $MEM 100  >/dev/null &
    echo "$!" > $PID_FILE
done

if [ $BONNIE -eq 1 ]; then
   echo "running bonnie"
   PID_NAME="sanity-$$-bonnie"
   PID_FILE="/var/run/${PID_NAME}.pid"
   if [ -e /tmp/foo ]; then
      /usr/sbin/bonnie++ -d /tmp/foo -u nobody:nobody -x 1000 >/dev/null &
      echo "$!" > $PID_FILE
   else
      mkdir /tmp/foo
      chmod 777 /tmp/foo
      /usr/sbin/bonnie++ -d /tmp/foo -u nobody:nobody -x 1000 >/dev/null &
      echo "$!" > $PID_FILE
   fi
fi

cat /var/run/sanity-$$-*

if [ $RAN -eq 1 ]; then
   MYTIME=`expr \( $RANDOM % $TFRAME \)  + 1`
   SIGN=`expr \( $RANDOM % 2 \)`
   if [ $SIGN -eq 1 ]; then
      SEC=`expr $SEC - $MYTIME`
   else
      SEC=`expr $SEC + $MYTIME`
   fi
   echo "Random time $SEC"
fi

sleep $SEC

for PID in `cat /var/run/sanity-$$-*`;
do
   echo "killing $PID"
   kill $PID
done

rm -rf /var/run/sanity-$$-*

exit 0
  • genip is a python script I used to generate series of IPs and zones on a windows dns server. This is mainly used when adding new racks of render blades.
#!/usr/bin/env python

import os
import sys
import getopt
import array
import re

def usage():
    print("usage:  genip -b [blade name] -n [nest name] -c [number of blades{16|32}] -d -l [blade start num] -s [gateway/type] -i -t\n\n")
    print("example:  genip -b imd-render -n 141 -c 16 -s 10.216.35.0/26\n")
    print("\t-d is optional, define nodes as doublewide which require A/B syntax")
    print("\t-l is optional, defaults to 1")
    print("\t-i is optional, if set, add a -c to the end of the name")
    print("\t-t is optional, test mode, show commands which would be run without running them")

def parseargs():
    global bladestart
    bladestart = 1
    global bladeletter
    # 0 is off, 1 is on
    bladeletter = 0
    global test
    test = 0
    global doublewide
    doublewide = 0
    global ilo
    ilo = 0
    try:
        opts, args = getopt.getopt(sys.argv[1:], "h:b:n:c:s:l:tdi", ["help"])
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt == "-b":
            global bladename
            bladename = arg
        elif opt == "-n":
            global nestname
            nestname = arg
        elif opt == "-c":
            global numblades
            numblades = arg
        elif opt == "-s":
            global subnet
            subnet = arg
        elif opt == "-l":
            bladestart = arg
            bladestart = int(bladestart)
        elif opt == "-t":
            test = 1
        elif opt == "-i":
            ilo = 1
        elif opt == "-d":
            doublewide = 1
    try:
        bladename
        nestname
        numblades
        subnet
    except:
        print "\nsome options were not set"
        print "bladename %s"%bladename
        print "nestname %s"%nestname
        print "numblades %s"%numblades
        print "subnet %s\n"%subnet
        usage()
        sys.exit()
    if int(numblades) != 16:
        if int(numblades) != 32:
            print "\nnumber of blades %s is not 16 or 32\n"%numblades
            usage()
            sys.exit()
    if int(numblades) == 32:
        bladeletter = 1
    elif doublewide == 1:
        bladeletter = 1

def parsesubnet():
    subparts = []
    subparts = subnet.split("/")
    global subbase
    subbase = subparts[0]
    global subtype
    subtype = subparts[1]
    subocts = subbase.split(".")
    global sub
    sub = subocts[0] + "." + subocts[1] + "." + subocts[2]
    global subreverse
    subreverse = subocts[2] + "." + subocts[1] + "." + subocts[0]
    global substart
        substart = int(subocts[3]) + 1
    global numips
        if subtype == '30':
        numips = 4
    elif subtype == '29':
        numips = 8
    elif subtype == '28':
        numips = 16
    elif subtype == '27':
        numips = 32
    elif subtype == '26':
        numips = 64
    elif subtype == '25':
        numips = 128
    elif subtype == '24':
        numips = 256
    else:
        print "subtype of %s not understood or in range" %subtype
        print "supported subtypes are 24-30"
        usage()
        sys.exit()
        #print "subbase %s subtype %s substart %d numips %d" % (subbase, subtype, substart, numips)
    subgate = int(subocts[3]) + 1

def zonetest():
    command = "dnscmd /zoneinfo " + subreverse + ".in-addr.arpa"
    if re.search('failed',  os.popen(command, 'r').readline() ):
        print "======== %s zone does not exist, creating =======" %subreverse
        command = "dnscmd /ZoneAdd " + subreverse + ".in-addr.arpa /dsprimary"
        if test == 1:
            print command
        else:
            os.popen(command, 'r')
    else:
        print "%s zone already created" %subreverse

def dnshost(bladename, nestname, bladenum, letter, sub, ip):
        if ilo == 1:
            commande = "dnscmd /enumrecords imagemoversdigital.com %s%s%02d%s-c"%(bladename, nestname, bladenum, letter)
        else:
            commande = "dnscmd /enumrecords imagemoversdigital.com %s%s%02d%s"%(bladename, nestname, bladenum, letter)
        #print commande
        if re.search('failed',  os.popen(commande, 'r').readline() ):
            adddnshost(bladename, nestname, bladenum, letter, sub, ip)
        else:
            if ilo == 1:
                commandee = "dnscmd /enumrecords imagemoversdigital.com %s%s%02d%s-c"%(bladename, nestname, bladenum, letter)
            else:
                commandee = "dnscmd /enumrecords imagemoversdigital.com %s%s%02d%s"%(bladename, nestname, bladenum, letter)
            #print commandee
            commandreturn = os.popen(commandee, 'r').readlines()
            ipinfo = commandreturn[1].split()
            #sometimes, /enumrecords will return sucessfull even though there is no IP
            if ipinfo[0] == "Command":
                                print "!!! Warning %s%s%02d%s exist but no ip is associated with it, trying anyway" %(bladename, nestname, bladenum, letter)
                adddnshost(bladename, nestname, bladenum, letter, sub, ip)
            else:
                curip = ipinfo[3]
                deletednshost(bladename, nestname, bladenum, letter, curip)
                adddnshost(bladename, nestname, bladenum, letter, sub, ip)

def adddnshost(bladename, nestname, bladenum, letter, sub, ip):
        if ilo == 1:
            commanda = "dnscmd /recordadd imagemoversdigital.com %s%s%02d%s-c A %s.%s"%(bladename, nestname, bladenum, letter ,sub ,ip)
            commandp = "dnscmd /recordadd %s.in-addr.arpa. %s PTR %s%s%02d%s-c.imagemoversdigital.com"%(subreverse, ip, bladename, nestname, bladenum, letter)
        else:
            commanda = "dnscmd /recordadd imagemoversdigital.com %s%s%02d%s A %s.%s"%(bladename, nestname, bladenum, letter ,sub ,ip)
            commandp = "dnscmd /recordadd %s.in-addr.arpa. %s PTR %s%s%02d%s.imagemoversdigital.com"%(subreverse, ip, bladename, nestname, bladenum, letter)
        if test == 1:
            print commanda
            print commandp
        else:
            os.popen(commanda, 'r')
            os.popen(commandp, 'r')

def deletednshost(bladename, nestname, bladenum, letter, curip):
        if ilo == 1:
            print "\t**** removing old ip %s for %s%s%02d%s-c ****"%(curip, bladename, nestname, bladenum, letter)
            commandd = "dnscmd /recorddelete imagemoversdigital.com %s%s%02d%s-c A %s /f"%(bladename, nestname, bladenum, letter, curip)
        else:
            print "\t**** removing old ip %s for %s%s%02d%s ****"%(curip, bladename, nestname, bladenum, letter)
            commandd = "dnscmd /recorddelete imagemoversdigital.com %s%s%02d%s A %s /f"%(bladename, nestname, bladenum, letter, curip)
        if test == 1:
            print commandd
        else:
            os.popen(commandd, 'r')

def genip():
    parseargs()
    parsesubnet()
    #print "bladename %s nestname %s numblades %s subnet %s substart %d numips %d" %(bladename, nestname, numblades, sub, substart, numips)
    intblades = int(numblades)
    numend = substart + intblades
    if intblades > numips:
        print "you asked to create %s dns entries with a /%s network which only has %d ips available,\nplease check your logic"%(numblades, subtype, numips)
        sys.exit()
    elif numend > 254:
        print "you asked to create %s starting at an ip of %d which is greater then 254 ips,\nplease rerun limited to a class c network"%(numblades, substart)
        sys.exit()
    zonetest()
    print "========== nest %s  =========="%nestname
    if bladeletter == 1:
        letter = "A"
        # if a double wide blade, then hold on cycle num for 2 loops
    else:
        letter = ""
    cyclenum = 0
    bladenum = bladestart
    for ip in range(substart, numend):
        print "==== adding %s%s%02d%s %s.%s to dns ===="%(bladename, nestname, bladenum, letter, sub, ip)
        dnshost(bladename, nestname, bladenum, letter, sub, ip)
        if bladeletter == 1:
            if cyclenum == 0:
                letter = "B"
                cyclenum = int(cyclenum) + 1
            else:
                letter = "A"
                cyclenum = 0
                bladenum = int(bladenum) + 1
        else:
            bladenum = int(bladenum) + 1

genip()

Comments are closed.