January 6, 2012 0

Installing Hubot as a Campfire bot on Ubuntu 10.04.2

By in SysAdmin
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:chris-lea/node.js

sudo apt-get update
sudo apt-get install nodejs nodejs-dev redis-server

#check to see if node installed
node -v

#install npm
curl http://npmjs.org/install.sh | sudo sh

#verify npm install
npm -v

cd /tmp

git clone https://github.com/github/hubot.git
cd hubot
npm install
sudo npm install -g coffee-script
make test

sudo cp -R /tmp/hubot /etc/.

export PATH="/etc/hubot/bin:$PATH"
export HUBOT_CAMPFIRE_TOKEN="<YOUR_BOTS_CAMPFIRE_API_TOKEN>"
export HUBOT_CAMPFIRE_ROOMS="<YOUR_CAMPFIRE_ROOM_NUMBER>"
export HUBOT_CAMPFIRE_ACCOUNT="<YOUR_CAMPFIRE_SUBDOMAIN>"

hubot -a campfire -n <YOUR_BOTS_CAMPFIRE_ACCOUNT_NAME> &

Tags: , ,

October 13, 2011 0

Multiprocessing SNMP with Python

By in Fun, SysAdmin

I’ve written a ton of snmp monitoring scripts and they all suck because they are blocking and take “too long” to return results for a large amount of hosts. So how would we make this process faster and make us happier?

multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. Due to this, the multiprocessing module allows the programmer to fully leverage multiple processors on a given machine. It runs on both Unix and Windows.

IBM has published a wonderful article on Multiprocessing with Python in which I have modified their snippets for my purposes below.

To leverage NetSNMP in python you need to compile it with python modules, I wrote a post about this yesterday in Compiling Netsnmp for Ubuntu. After battling through the compiling bit you should be able to get the example in the IBM post to work. I modified this example *slightly* to accommodate instance identifier (iid).  Hopefully this will be suitable for a new project I’m working on which polls thousands of snmp oids to measure latency between routers using the concept I posted on hatch – Configure RTR/SLA Ping in IOS template.

 

!/usr/bin/env python
"""
This is a multiprocessing wrapper for Net-SNMP.
This makes a synchronous API asynchronous by combining
it with Python2.6
"""

import netsnmp
from multiprocessing import Process, Queue, current_process

class HostRecord():
    """This creates a host record"""
    def __init__(self,
                 hostname = None,
                 query = None):
        self.hostname = hostname
        self.query = query

class SnmpSession():
    """A SNMP Session"""
    def __init__(self,
                oid = "sysDescr",
                iid="0",
                Version = 2,
                DestHost = "localhost",
                Community = "public",
                Verbose = True,
                ):
        self.oid = oid
        self.Version = Version
        self.DestHost = DestHost
        self.Community = Community
        self.Verbose = Verbose
        self.var = netsnmp.Varbind(oid, iid)
        self.hostrec = HostRecord()
        self.hostrec.hostname = self.DestHost

    def query(self):
        """Creates SNMP query

        Fills out a Host Object and returns result
        """
        try:
            result = netsnmp.snmpget(self.var,
                                Version = self.Version,
                                DestHost = self.DestHost,
                                Community = self.Community)
            self.hostrec.query = result
        except Exception, err:
            if self.Verbose:
                print err
            self.hostrec.query = None
        finally:
            return self.hostrec

def make_query(host):
    """This does the actual snmp query

    This is a bit fancy as it accepts both instances
    of SnmpSession and host/ip addresses.  This
    allows a user to customize mass queries with
    subsets of different hostnames and community strings
    """
    if isinstance(host,SnmpSession):
        return host.query()
    else:
        s = SnmpSession(DestHost=host)
        return s.query()

# Function run by worker processes
def worker(input, output):
    for func in iter(input.get, 'STOP'):
        result = make_query(func)
        output.put(result)

def main():
    """Runs everything"""

    #clients
    hosts = [
    SnmpSession(DestHost="10.71.1.1", Community="my-pub-community", oid="1.3.6.1.4.1.9.9.42.1.2.10.1.1", iid="1"),
    SnmpSession(DestHost="10.81.1.1", Community="my-pub-community", oid="1.3.6.1.4.1.9.9.42.1.2.10.1.1", iid="123")
    ]
    NUMBER_OF_PROCESSES = len(hosts)

    # Create queues
    task_queue = Queue()
    done_queue = Queue()

    #submit tasks
    for host in hosts:
        task_queue.put(host)

    #Start worker processes
    for i in range(NUMBER_OF_PROCESSES):
        Process(target=worker, args=(task_queue, done_queue)).start()

     # Get and print results
    print 'Unordered results:'
    for i in range(len(hosts)):
        print '\t', done_queue.get().query

    # Tell child processes to stop
    for i in range(NUMBER_OF_PROCESSES):
        task_queue.put('STOP')
        #print "Stopping Process #%s" % i

if __name__ == "__main__":
    main()
October 12, 2011 1

Compiling Netsnmp for Ubuntu with Python Bindings

By in SysAdmin

apt-get install gcc
apt-get install libperl-dev
apt-get install python2.7-dev

Download the netsnmp source:

wget http://downloads.sourceforge.net/project/net-snmp/net-snmp/5.7.1/net-snmp-5.7.1.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fnet-snmp%2Ffiles%2Fnet-snmp%2F5.7.1%2F&ts=1318446955&use_mirror=surfnet
tar xzvf net-snmp-5.7.1.tar.gz
cd net-snmp-5.7.1

./configure –with-python-modules

Answer the questions

make

make install

ldconfig

akonkol@cmg005:/home/akonkol/net-snmp-5.7.1# python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import netsnmp
>>>

August 22, 2011 0

Creating Share Buttons for Twitter, Facebook, and Linkedin using Django Templatetags

By in Uncategorized

It’s easy to copy and paste urls to share links, but its easier to just click on a button to do it. The way I decided to do it was to write a custom django templatetag. The template tag exists in myproject/myapp/templatetags/sharebuttons.py.

You can use the pre-built template here

Usage in a template:

{% load sharebuttons %}

{% for p in posts %}
   {% sharebutton p "twitter" %}
   {% sharebutton p "facebook" %}
   {% sharebutton p "linkedin" %}
{% endfor %}
August 17, 2011 0

Tweeting from a Django Application

By in Fun, Nothing, SysAdmin

It’s easy to get your application to start setting twitter status updates in django.

  1. Create a twitter account twitter.com
  2. Create a twitter “application’ and get your keys dev.twitter.com/apps
  3. Click on “settings” and set the access to read and write
  4. easy_install python-twitter
  5. Use this template

Whenever you save a “Post” object, the def send_tweet method will attempt to tweet a message containing the name of the object as well as the absolute url.

Tags: ,