Archive for the ‘Life the Universe and Everything’ Category

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.

SysInfoRM Git Repository

By Mark Davidson on February 11th, 2010

Just a very quick update I have setup a Git Repository for my SysInfoRM project.

It can be found at http://github.com/mtdavidson/SysInfoRM/

Installing & Configuring Cacti Under Gentoo

By Mark Davidson on February 9th, 2010

Cacti is a front end to RRDTool, the purpose of which to provide an effective network graphing solution for monitoring devices within a Network. It can be used with SNMP to monitor various statistics about a device including but not limited to Load Average, Bandwidth, Disk Usage and Processes.

The following are the steps to install Cacti under Gentoo

  1. Modify your /etc/make.conf and modify your use flags adding “mysql xml sockets vhosts”, which should give you a line reading something similar to
    USE="symlink mmx sse sse2 bash-completion vhosts xml sockets snmp"
  2. Now emerge Apache, PHP, Cacti and webapp-config. You may already have some of these installed but it is important to rebuild them with the new use flags.
    sudo emerge apache php cacti webapp-config

    Once completed if everything has installed correctly procede to the next step if you get an error saying “Could not read settings from webapp-config” I found the easiest way to solve this was to unmerege webapp-config and reinstalled it.

  3. Update your /etc/ config files if required
    sudo etc-update
  4. Create a vhost if you don’t already have one and then run the following. Then install cacti to the vhost using webapp-config. Remember to change the -h option to reflect the name of your vhost and that you may need to set a different cacti version number if cacti has been updated since I posted this article.
    sudo webapp-config -I -h yourdomain.com -d cacti cacti 0.8.7e-r1
  5. Its now time to setup the database that Cacti will be using.
    mysqladmin -p --user=root create cacti
    mysql -p --user=root cacti < /var/www/yourdomain.com/htdocs/cacti/cacti.sql # Remember to change this to reflect the path to your cacti install.
    mysql -p --user=root mysql
    GRANT ALL ON cacti.* TO cactiuser@localhost IDENTIFIED BY  'somepassword'; # Just a note I like to use apg to generate my passwords.
    flush privileges;
  6. Now that the database has been created your need to set the database settings in cacti. Modify /var/www/yourdomain.com/htdocs/cacti/include/config.php if your installing with a local database and only changed the password above that’s all you need to update in the config file.
  7. The last step of the install is to add a cron entry to your tab to get Cacti to update. Add the following entry to your crontab updating the path as needed.
    */5 * * * * apache /usr/bin/php  /var/www/yourdomain.com/htdocs/cacti/poller.php > /dev/null  2>&1
  8. That should be it for Cacti base install visit http://yourdomain.com/cacti and you should be meet with a login screen use admin as the username and admin as the password. You should now see the Cacti inteface.
  9. Click on the Graphs tab accross the top and after a while once data starts coming in your should see the graphs start to be drawn. At the moment these graphs will display localhost data.

Thats all for now in the next post I will cover setting up net-snmpd on a host and then configuring Cacti to monitor it.