Thursday, June 12, 2008

Get system load using Python Script

I have written a simple python script that collect system load information and store in a XML file.


#!/usr/bin/env python

import os
import commands
import time
from time import gmtime, strftime

while 0 < 10:

file_name = strftime("%Y_%m_%d_%H_%M", gmtime())

data = commands.getoutput("w | grep load")
xml_data = ""+ data + ""

print xml_data


if os.path.isfile(file_name + ".xml"):
x=0
else:

f_prev=open(file_name + ".xml", 'a')
f_prev.write("")
f_prev.close

f=open(file_name + ".xml", 'w')
f.write("\n\n")
f.close()


f=open(file_name + ".xml", 'a')

f.write(xml_data+"\n")

f.close()
time.sleep(3)



We will get XML file like this.
<?xml version='1.0' encoding='utf-8'?>
<data>
<load> 01:40:38 up 6:27, 3 users, load average: 0.07, 0.07, 0.03</load>
<load> 01:40:48 up 6:27, 3 users, load average: 0.06, 0.07, 0.03</load>
<load> 01:40:58 up 6:27, 3 users, load average: 0.05, 0.07, 0.03</load>
</data>

Monday, June 09, 2008

putpixel in Linux

Some days ago I was trying to read a BMP format file and show it using putpixel.

I haved used qdbmp.sourceforge.net which is a minimalistic cross-platform C library for handling BMP image file.

The Allegro (www.allegro.cc) graphics library that provides many functionality.

putpixel
Writes a pixel into a bitmap.
Description void putpixel(BITMAP *bmp, int x, int y, int color);

Download qdbmp.c qdbmp.h from the qdbmp website


Sample code that reads a BMP file and show it using Allegro library

/*
* Example program for the Allegro library, by Shawn Hargreaves.
*
* This is a very simple program showing how to get into graphics
* mode and draw text onto the screen.
*/

#include <allegro.h>
/* Creates a negative image of the input bitmap file */
#include "qdbmp.h"
#include <stdio.h>

int main( int argc, char* argv[] )
{
BMP* bmp;
UCHAR r, g, b;
UINT width, height;
UINT x, y;

int color;
/* you should always do this at the start of Allegro programs */
if (allegro_init() != 0)
return 1;

/* set up the keyboard handler */
install_keyboard();

/* set a graphics mode sized 320x200 */
if (set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0) != 0) {
if (set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) != 0) {
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
return 1;
}
}

/* set the color palette */
set_palette(desktop_palette);

/* clear the screen to white */
clear_to_color(screen, makecol(255, 255, 255));

/* you don't need to do this, but on some platforms (eg. Windows) things
* will be drawn more quickly if you always acquire the screen before
* trying to draw onto it.
*/
acquire_screen();

/* write some text to the screen with black letters and transparent background */
// textout_centre_ex(screen, font, "Hello, world!", SCREEN_W/2, SCREEN_H/2, makecol(0,0,0), -1);


if ( argc != 3 )
{
fprintf( stderr, "Usage: %s <input file> <output file>\n", argv[ 0 ] );
return 0;
}

/* Read an image file */
bmp = BMP_ReadFile( argv[ 1 ] );
BMP_CHECK_ERROR( stderr, -1 ); /* If an error has occurred, notify and exit */

/* Get image's dimensions */
width = BMP_GetWidth( bmp );
height = BMP_GetHeight( bmp );

/* Iterate through all the image's pixels */
for ( x = 0 ; x < width ; ++x )
{
for ( y = 0 ; y < height ; ++y )
{
/* Get pixel's RGB values */
BMP_GetPixelRGB( bmp, x, y, &r, &g, &b );

color = makecol(r,g,b);

putpixel(screen, x, y, color);

/* Invert RGB values */
BMP_SetPixelRGB( bmp, x, y, 255 - r, 255 - g, 255 - b );
}
}


// putpixel(screen, SCREEN_W/2, SCREEN_H/2, 50);

/* you must always release bitmaps before calling any input functions */
release_screen();

/* wait for a key press */
readkey();

/* Save result */
BMP_WriteFile( bmp, argv[ 2 ] );
BMP_CHECK_ERROR( stderr, -2 );

/* Free all memory allocated for the image */
BMP_Free( bmp );

return 0;
}

END_OF_MAIN()

Compile
$gcc -g -O2 -o name `allegro-config --libs` sample.c qdbmp.c


Resources:
http://qdbmp.sourceforge.net/
http://www.allegro.cc/manual/api/drawing-primitives/putpixel
http://www.allegro.cc/manual/api/truecolor-pixel-formats/makecol