Posts Tagged ‘python’

VIM Python Code Folding

By Mark Davidson on February 11th, 2010

Today I have been working on my new project SysInfoRM and for this I am using Python as my language of choice and VIM as my editor. In an attempt to improve my experience with using VIM I have been checking out code folding after a bit of digging around I tracked down a good vimrc file. I found a good post on Amitu Blog detailing the vimrc and other associated details. Unfortunately the site was down when I visited and I had to rely on the version that is cached by Google.

So I thought I would take a few points from that blog post and expand on it a bit in case the copy in Google’s cache copy goes as well. I give full credit to the original author and would like to thank him for his time and effort on the original post.

Here are the important new lines for your .vimrc

" automatically save and restore folds
au BufWinLeave * mkview
au BufWinEnter * silent loadview

" this lets us put the marker in the file so that
" it can be shared across and stored in version control.
set foldmethod=marker
" this is for python, put
" # name for the folded text # {{{
" to begin marker and
" # }}}
" close to end it.
set commentstring=\ #\ %s
" default fold level, all open, set it 200 or something
" to make it all closed.
set foldlevel=0

" share clipboard with windows clipboard
set clipboard+=unnamed

" set viminfo='100,f1
" minibufexplorer settings:j
let g:miniBufExplMapWindowNavArrows = 1
let g:miniBufExplMapCTabSwitchWindows = 1

These are the keyboard command for using the folds.

  • zf create the fold, useful for manual and marker methods. Select any piece of text, [press v or shift-v, then use arrow keys], and then press zf. It will place the markers around the fold for you in marker mode; in case of manual, it will store fold location in memory. Remember f by saying this command “forms” the fold, or just remember fold :-)
  • zc close the fold at the cursor.
  • zo open the fold at the cursor.
  • zr increment the fold level by one, so if all classes are folded, they will opened, but function definitions will be kept folded.
  • zm reverse of the above, if one or more function folds are open, they will be closed, but classes will be kept open.
  • zR open all folds.
  • zM close all folds.
  • zj and zk can be used to jump from one fold to another.

Here is an example of what should expect. This is some example Python code.

from xml.parsers.xmlproc import xmlproc
from xml.parsers.xmlproc import xmlval
from xml.parsers.xmlproc import xmldtd

# XML Structure Checking # {{{
class MyApp(xmlproc.Application):
  def handle_start_tag(self,name,attrs):
    pass
  def handle_end_tag(self,name):
    pass
  def handle_data(self,data,start,end):
    pass
  def handle_comment(self,data):
    pass
# }}}

Placing the cursor at and point after line 5 then using zc, would result in.

from xml.parsers.xmlproc import xmlproc
from xml.parsers.xmlproc import xmlval
from xml.parsers.xmlproc import xmldtd

+-- 11 lines: XML Structure Checking-----------------------------------------------

That is all for now I plan to expand a bit on these vimrc modifications tomorrow and will then publish my entire vimrc which is becoming quite extensive.

Remote Server Monitoring using phpSysInfo XML – SysInfoRM Concept

By Mark Davidson on February 10th, 2010

Recently I have been checking out a number of different solutions for monitoring remote servers and alerting or warning depending on certain metrics. For this I have looked at a number of different solutions including Server Density, Cloudkick, Cacti, Nagios and some others. Each of these has various advantages and disadvantages.Which I will give a very brief run down of below.

Server Density
The Good

  • iPhone Application
  • Push Notifications for iPhone Application
  • Easy to Deploy Agent

The Bad

  • Costs Per Server for Full Version
  • Limited Services it can monitor.

Cloudkick
The Good

  • Incredibly easy to setup monitoring of multiple hosts if your with one of the supported providers. I was using vps.net when I tried them out and it worked very well.
  • Very good metric monitoring.
  • Very nice interface.

The Bad

  • Limited to monitoring of supported providers.
  • Need to hand over API key for it to work.
  • In my opinion incredibly expensive.

Cacti
The Good

  • SNMP Integration
  • Great Interface
  • Very good metric support
  • Cost – Free

The Bad

  • Not Really a Remote Monitoring Solution as does not provide alerting. I know that it was never designed to be but definitely has potential to be expanded on.

Nagios
The Good

  • Highly Configurable
  • Support for Custom Monitoring Scripts
  • Great Alerting Configuration
  • Cost – Free

The Bad

  • Complicated to configure even for basic monitoring
  • Runs on single host meaning all network monitoring lost of host goes down.

Since none of the above solutions exactly suite my needs I have decided to produce my own monitoring solution. To do this I am going to take advantage of the fact that phpSysInfo provides the majority of the statistics that I wish to monitor and I already run it on most of my servers. phpSysInfo can supply its data in XML form this is that data I can use for monitoring.
So I plan to develop my own solution in Python to read the XML data from multiple remote hosts and then take a defined action if a rule is matched. Since I needed a name for this project I decided to call it SysInfoRM or System Information Remote Monitor.

Here is the basic feature plans for now

Initial Release Features for SysInfoRM

  • Parsing of 1 – n XML from either phpSysInfo or pySysInfo (Since it can supply data in same XML format and should be good for remote monitoring of non web hosts).
  • Ability to define monitoring rules and actions for when they are matched.
  • Easy / Understandable Configuration.
  • Heart Beat Monitoring of Script Itself. Basically run the script in two locations if the Primary fails the other should take over the monitoring.
  • Can be run as a Daemon or as interactive script.

Future Planned Features for SysInfoRM

  • Curses interface for viewing and configuration.
  • Web interface for viewing and configuration.

Python Libraries

  • urllib2
  • smtplib

I am hoping to have something working in the next couple of days just need to get my Python skills up to scratch. I am going to attempt to incorporate all the best bits from the other currently available monitoring tools and as few of the bad bits as possible. I should also mention I was inspired by and am drawing on another good monitoring script from the tomubuntu blog. Tom has done a really good job with this script and I suggest people check it out its a good simple script that can monitor Load Average, Memory and other stats; it can then alert using sSMTP.

That is all for now please check back soon I’ll be providing updates on the progress of SysInfoRM as I develop it and will post the SVN or Git Repos once I get one set up.