Home     Wordpress     Codex

Temperature sensors on Dell Poweredge 850 in Linux

June 29th, 2009 by admin | 2 Comments | Filed in Tech

This is what I had to do to get some temperature reading from a Dell P850 in Ubuntu Linux.

Loading the IPMI kernel modules

root@server:~# modprobe ipmi_devintf
root@server:~# modprobe ipmi_si

Creating the device unless it’s already made
(I.e. if /dev/ipmi0 doesn’t already exist, do this step.)

root@server:~# mknod /dev/ipmi0 c 252 0

Installing the tools and reading temperatures

This will install the ipmitool package

root@server:~# apt-get install ipmitool

Read temperatures

root@server:~# ipmitool sdr type Temp
Temp | 01h | ok | 3.1 | 38 degrees C
Planar Temp | 04h | ok | 7.1 | 28 degrees C
Temp Interface | 53h | ns | 7.1 | Disabled

I wrote a munin plugin to parse and graph this too, shout if it’s interesting!

Tags: , , , , ,

Learning PHP (6) – Connecting to a database

June 16th, 2009 by admin | 4 Comments | Filed in Learning PHP

Everyone needs to store and get data from a database every now and then. MySQL can be an excellent choice for some.

Creating a table
This is how you create a simple table from the CLI, you can recognize the values if you use systems like phpmyadmin for modifying database structures.

mysql> CREATE TABLE mytable (
-> id INT NOT NULL AUTO_INCREMENT,
-> name VARCHAR(255) NOT NULL,
-> PRIMARY KEY(id)
-> );
Query OK, 0 rows affected (0.02 sec)

The MySQL reserved are upper cased, all the text in lower case are things
that you can change. This basically creates a table named ‘mytable’ with
two columns; “id” and “name”, id is an INT(eger) and name is a varchar of
max 255 characters.

AUTO_INCREMENT means that id will be automatically incremented if not defined.

Connecting to MySQL from PHP
Disclaimer: This is a very basic example, if you are coding for production use please at least look at the function mysql_real_escape_string() or a library
that will handle insertion and such.

The example above inserts a record in MySQL, the id will be automatically set to 1 (for the first row you insert, 2 for the next, etc.). To read this record out from the database again you can do something like this:

Data from MySQL

This piece of code will perform a “SELECT * FROM mytable” query (the * means every column from the table, you could get only the name by doing “SELECT name FROM mytable“.
Since there can be multiple rows we perform a while loop, so while $row gets fed with data from the mysql_fetch_assoc() function it will print the ‘id’ and ‘name’ columns from the table.

Select specific row(s) only (match on name)

Data from MySQL

Now we used the LIKE keyword in MySQL, the percent (%) sign is a wildcard.
This will get every row from the table where name begins with an E.

Homework: Now try to combine $_GET and $_POST + MySQL. :)

Tags: ,

Learning PHP (5) – Using built-in functions

June 12th, 2009 by admin | No Comments | Filed in Learning PHP

I had promised myself that I would post every second day, but I didn’t know exactly where to go from what we have built up and I also became engaged, she said yes! So it has been crazy days.
But I’m happy, at least :-D

I am going to continue with showing how to take use of some of the built-in string fuctions. These can be used to manipulate text variables that you might receive from the $_GET and $_POST variables.

Find occurrence of text in strings
You can for example use the function strstr() or stristr() if you want a cast-insensitive matching style.

Example

$mystring = 'this websites owner is nice';
if (strstr($mystring, 'website')) {
 echo 'I found the word "website" in the string.';
}

I can use double quotes in the string because I have it enclosed in single quotes.
If I were to use single quotes it would unquote the string and generate a syntax error.

If you replace strstr with stristr in the example, it would
also give a match if $mystring contains the word ‘Website’ with an upper case W.

explode() strings
If you get a string such as “one,two,three,four,five,six” and you would rather have
it indexed in an array you can use the function explode() to achieve this.

The build-in function explode() will parse a string and split it based on the
argument you give the function. An example will follow:

$mystring = "one,two,three,four;five,six,seven";
$myarray = explode(",", $mystring);

var_dump($myarray);

The first argument to explode is the delimiting character, while the other
is the string that will be split into an array. The result of this would be:

array(6) {
[0]=>
string(3) “one”
[1]=>
string(3) “two”
[2]=>
string(5) “three”
[3]=>
string(9) “four;five”
[4]=>
string(3) “six”
[5]=>
string(5) “seven”
}

Notice that $myarray[3] contains four;five, this is due to a common
mistake.. it can be hard to overlook, so can you figure out why it does that? :-D

Anyways, you should check out the documentation and try out some
of the functions, because they are really easy to use and implement.

Good luck!

Tags: , , , , , , , ,

Learning PHP Part Four – If and else

May 30th, 2009 by admin | No Comments | Filed in Learning PHP

Have you been playing around with the GET and POST arrays now?
If not, learn about foreach in PHP here.

What can I do now?
You can go ahead and use if to check the contents of variables.
Check this out.

If
The syntax for if goes like this:

if (statement) {
  Insert your code here
}

The statement will be checked, and if it is true the code within the { and } will be executed.
Take for example a script where you get a variable from the url, take the previous
example where you used $_GET['name'] to retrieve the ?name= part of the URL.

Now, if your name is John and you want the page to say hello to you but not to anyone else.


The $_GET['name'] == ‘John’ part is called the statement,
you probably understand it but it checks to see if the $_GET array variable
with the key ‘name’ contains a string ‘John’.

You can do similar tests with numbers, but then you won’t need the quotes
like the ones I have around ‘John’.

The example will then be:


Let us say you want to check if the name is NOT John.

You're not my master.

Else
Now, take the example where you output ‘Hi master’, what if you want
to print something to the ones not being the master? You can use else

You're not my master

That’s all for today, I don’t have so much time today so I am going to keep it quite short.
I will get back with a new article in these series in a couple of days, hope I’ll see you back here then!

Tags: , , ,

Really cool Spotify and last.fm Mashup

May 29th, 2009 by admin | No Comments | Filed in mashups

Just a short post for today, but I thought I’d make you aware of an application (that has been around for a while, but hey, it’s creative).

If you use Spotify and Last.fm this mashup of an application is a great way to find music on added to Spotify that matches your top 50 artists (or so, Vilhelm writes.)

Maybe you can find some goodies there!

Tags: , , ,

Learning PHP (3); Reserved vars and loops

May 28th, 2009 by admin | 1 Comment | Filed in Learning PHP

In my previous article I explained arrays and their use. But have you ever wondered what happens to those variables set in the url? They are contained within an array which is prefilled for your scripts.

The array variables $_GET, $_POST and $_SERVER are some of the interesting ones you can use.

The reserved $_GET variable
An example would be this type of URL.
http://www.example.domain/myphpscript.php?name=Espen

To catch the variable ‘name’ you can do this piece of PHP

<?php
echo “Hi ” . $_GET['name'];
?>

This will output “Hi Espen”.

Using the reserved $_POST variable
To get the variables from a form you can use $_POST['key'].
Look at this example:

Create this HTML form, change the path to myhelloscript.php to a place where you test your scripts.

<form action=”http://my.domain/myhelloscript.php” method=”post”>
<input type=”text” name=”name”>
<input type=”submit” value=”send it”>
</form>

Then create the file myhelloscript.php with the following contents

<?php
echo “hi ” . $_POST['name'];
?>

You see? The same result here, only now we’re using POST instead of GET (you can also notice that the URL does not contain the variables).

Looping over an array
Looping over an array can be very usefull in the case where you don’t know what the reference keys are.
Let us say you didn’t know that the key for the name in the previous post example was not called¬† ‘name’, how would you find it?

Here is an example of looping over an array, looping through all the key and value pairs.

<?php
$myarray = array(‘Espen’, ‘John’, ‘Lisa’, ‘Mark’);

foreach ($myarray as $value) {
echo $value . “<br>”;
}
?>

Now we’re saying for each ‘entry’ in $myarray get the entry as $value and do something with it between the { and }
So for each $myarray as $value: echo $value and a ‘<br>’ (to get a newline).

This example will output

Espen
John
Lisa
Mark

Getting the key
But that’s not the key, is it?¬† How do you find the keys?
This example will use the foreach command in another way to get the keys also

<?php
foreach ($myarray as $key => $value) {
echo $key . ‘ is ‘ . $value . ‘<br>’;
}
?>

Now you can also use the variable $key within the loop for each value to get the corresponding key.

This is an example of looping over the $_GET array

<?php
foreach ($_GET as $key => $value) {
echo $key . ‘ is set to ‘ . $value . ‘<br>’;
}
?>

A Challenge
The $_SERVER variable contains a lot of variables about the request, can you figure out how to show them all?

If you get any weird syntax errors or such, post them as a comment and I will try to help you out!

Tags: , , , , ,

Learning PHP – Part two; Arrays

May 26th, 2009 by admin | 1 Comment | Filed in Learning PHP

Here’s my follow up on my PHP articles, this time I am going to talk a bit about arrays. They are so important that you should learn about them sooner than later.

What are arrays?
This quote is from wikipedia.org

An array is a systematic arrangement of objects, usually in rows and columns. Specifically, it may refer to several things.

That’s exactly what it is, it’s a variable (see previous post) containing several values (like a shelf with several compartments) that can be retrieved by a key (the number on the compartment). Array keys are per default a number, but it can also be an associative array with text strings for keys.

So make an array and fill it with data
Here is an example on how to declare an array:

<?php
$bookshelf = array(
‘old books’,
‘new books’,
‘cooking books’
);
?>

The variable name is $bookshelf, it is set to be an array(). The contents is put in between the (), but you can also declare an empty array by doing this:

$bookshelf = array();

By default, the first value will have the key 0, the second will have 1, etc.
To refer to a value within an array and echo it out to display:

<?php
$bookshelf = array(
‘old books’,
‘new books’,
‘cooking books’
);

echo $bookshelf[0];

?>

The result outputted from this script will be:

old books

By using the [ and ] signs behind a variable you are referring to it as an array, what you put between them is the key you are using. Try to change the value between them to 1 and check the result.

Adding values to a PHP array
You can make use of the array_push() function to insert more variables to a previously declared array.

<?php
$bookshelf = array(
‘old books’,
‘new books’,
‘cooking books’
);

array_push($bookshelf, ‘dvds’);

echo $bookshelf[3];

?>

You can also set a variable directly by using the key

<?php
$bookshelf = array(
‘old books’,
‘new books’,
‘cooking books’
);

$bookshelf[3] = ‘dvds’;

echo $bookshelf[3];

?>

Deleting a value from a PHP array
Several ways to this also, you can use array_pop() to unset the last value in the array.
The unset() function can be on the array value to unset it.
You can set it to null by referring to the key

array_pop($bookshelf);
unset($bookshelf[3]);
$bookshelf[3] = null;

While these are regular arrays, there are other possible ways to use this. A string variable like $name = “Espen”; in my previous article is actually an array of characters. $name[0] will refer to ‘E’, 1 will refer to ‘s’ so on and so forth.

Associative Arrays in PHP
You can use keys which are not numbers in an array, those are called associative arrays.
Here is an example, I bet you understand most of the basics now, and in case NOT – drop me a comment!

$bookshelf['favourite'] = ‘Marketing 911′;
echo $bookshelf['favourite'];

In my next article you will get magic behind the array variables $_GET, $_POST and $_SERVER to finally be able to handle user input from forms and every secret of the request. Want to know when I post it? Get Learning by RSS.

Tags: , ,

Learning PHP – Part one

May 24th, 2009 by admin | 4 Comments | Filed in Learning PHP

Do you want to learn PHP?

I’ve been coding (or scripting if you want) PHP actively for myself for probably 10 years now, over the years I’ve had a lot of questions about it and have done a lot of small favours for people. I would be happy to give away some of my knowledge, so please ask if there is anything.

I am going to teach PHP from basic to advanced, I will try to keep it to one article every other day.
Subscribe to my feed if you don’t want to miss out on this chance.

Basic Introduction
What is PHP? It is a scripting language that resides on the server-side as opposed to javascript which is downloaded and ran by the client (which is called client-side).

To be able to execute PHP scripts, you need access to a webserver that runs PHP scripts – there are a lot of alternatives.
I am not going to explain them here, but I am sure it is possible to arrange something if you need a place and is into learning.
It is also possible to run an Apache server locally with PHP.

My First PHP Script
Create a file named first.php and this is the contents

<?php
echo “hello (word)”;
?>

Save this file somewhere on the webserver and try to go to the url.
It will display: hello (world)

Explanation
The first line is <?php, this tells the webserver that it is about to parse PHP scripting. You need this before every piece PHP script for it to be executed as PHP.

The second line is echo “hello (world)”;, this is actually the PHP script line.
It uses the built-in function ‘echo’ to display a string, the string needs to be contained within quotes.
And notice that the ending of the line is ;, this finishes off the statement.
If you were to forget the ; and had several lines of code the server will continue to feed the rest of the code into the echo function, which would probably not work or give really really unexpected results.

The third line is ?>, this tells the webserver that the PHP script is over.
When it reaches this tag, it will continue to output the rest of the page to the browsing user. This means that you can use PHP in between HTML to generate dynamic responses.

My Second PHP Script
Heck, since it’s the first post and the first script was so easy.. I will show you another script and teach how variables work.
I will move on to more advanced things later, so keep watching if this is way too basic for you. There will be more blood! :D

<?php
$name = “Espen”;
$age = 23;
$twice = $age * 2;
echo “My name is ” . $name . ” and my age is ” . $age . “, if I had lived twice as long I’d be ” . $twice;
?>

Explanation
The first line of PHP script is $name = ‘Espen’;.
The dollar sign ($) indicates that this is a variable, variables are used to contain dynamic information like if you want to display it at several places in your document.

The variable name is actually ‘name’, and it is set to contain anything behind the equal (=).
The string behind the equal sign is “Espen”, notice the ; ending again.
Because it is contained within quotes, PHP automatically identifies it as a string, it will not work without quotes.
I will show some examples between the difference in double and single quotes later.

The second line is $age = 23;.
As before, notice the dollar sign and also notice the ; ending.
Because age is an integer, you don’t need quotes. If you use quotes around the age like ’23′, PHP will identify it as a string and doing calculations on it can fail.

The third line is $twice = $age * 2;
This sets the variable named $twice to contain whatever is in the variable $age and multiply it with two.
We know from before that $age is set to 23, so this will yield a result of 46. $twice now contains 46.

The fourth line is:
echo “My name is ” . $name . ” and my age is ” . $age . “, if I had lived twice as long I’d be ” . $twice;

One of the methods of combining variables into another is the usage of the period character (.) There are other and better ways to do it, but that may be to complicated for a beginner for now.
Some if you may relate this to javascript where they use + to combine variables.
So this actually feeds a lot of parts of information to the echo function to display it:
“My name is ” is the first part, the next variable will be printed right after it – so notice the white space before the last qutoe.
Then the period character is used to append the variable $name to it.
The string ” and my age is ” is appended by . again, notice white spaces before and after the string.
You probably understand the rest of it now.
Notice that any white space added that is not contained within quotes will not be displayed either.

If anything is unclear or completely wrong, feel free to drop me a comment!
More learning is coming: Subscribe to my feed.

Tags: , , , ,

Google Maps in Flash with the Flex SDK and haXe

May 23rd, 2009 by admin | No Comments | Filed in mashups

and getting it to work together.. seems like people have a lot of issues with this, so here are the steps you have to take.

If you do not have an API key, sign up for one here.

This way of installing haXe is pretty bad and should be avoided, but it made it easy swapping haXe versions fast.
I use the haXe version 1.19 together with the Google Maps Flash API.

Lets first get haXe and the Googles Flash API

root@devbox:/usr/local/src$ mkdir flashdevelopment
root@devbox:/usr/local/src$ cd flashdevelopment/
root@devbox:/usr/local/src/flashdevelopment$ wget -q \

http://maps.googleapis.com/maps/flash/release/sdk.zip

root@devbox:/usr/local/src/flashdevelopment$ wget -q \

http://haxe.org/file/haxe-1.19-linux.tar.gz

root@devbox:/usr/local/src/flashdevelopment$ unzip -q sdk.zip
root@devbox:/usr/local/src/flashdevelopment$ tar -zxf \
haxe-1.19-linux.tar.gz

To make haXe work:

root@devbox:/usr/local/src/flashdevelopment$ cd haxe-1.19-linux
root@devbox:/usr/local/src/flashdevelopment/haxe-1.19-linux$ cp haxe /usr/bin/
root@devbox:/usr/local/src/flashdevelopment/haxe-1.19-linux$ cp haxelib /usr/bin
root@devbox:/usr/local/src/flashdevelopment/haxe-1.19-linux$ cp haxedoc /usr/bin/
root@devbox:/usr/local/src/flashdevelopment/haxe-1.19-linux$ mkdir /usr/lib/haxe
root@devbox:/usr/local/src/flashdevelopment/haxe-1.19-linux$ ln -s /usr/local/src/flashdevelopment/haxe-1.19-linux/std/ /usr/lib/haxe

Now we need to generate hxclasses from the SDK to make it usable in haXe

root@devbox:/usr/local/src/flashdevelopment$ cd lib
root@devbox:/usr/local/src/flashdevelopment/lib$ mv map_1_9.swc map_1_9.zip
root@devbox:/usr/local/src/flashdevelopment/lib$ unzip map_1_9.zip
Archive: map_1_9.zip
inflating: library.swf
inflating: catalog.xml
root@devbox:/usr/local/src/flashdevelopment/lib$ haxe –gen-hx-classes library.swf

Now, there are some changes that has to made to make haXe like the API
I generated the working sources with a newer version of haXe, and applied the changes to it.
This makes the patchfile a bit larger than it had to be, because I had to make a patch against my haxe 1.19-generated hxclasses/ folder.
Anyways, this works and it is against the 1.9 file from Google.

root@devbox:/usr/local/src/flashdevelopment/lib$ wget -q \

http://arpa.no/googlemaps.diff

root@devbox:/usr/local/src/flashdevelopment/lib$ patch -p0 < \
googlemaps.diff

Clean up a bit so we can just develop here:

root@devbox:/usr/local/src/flashdevelopment$ rm lib/map_*
root@devbox:/usr/local/src/flashdevelopment$ rm haxe-1.19-linux.tar.gz sdk.zip

Lets try to create just a simple map, this file will be /usr/local/src/flashdevelopment/MyMap.hx

import com.google.maps.Map;
import com.google.maps.MapEvent;
import com.google.maps.LatLng;
import com.google.maps.MapType;
import com.google.maps.controls.ZoomControl;
import com.google.maps.controls.MapTypeControl;
import com.google.maps.controls.PositionControl;

class MyMap extends Map {
static function main() {
var app:MyMap = new MyMap();
flash.Lib.current.addChild(app);
app.setSize(new flash.geom.Point(400,400));
}

public function new(){
super();
addEventListener(MapEvent.MAP_READY, onMapReady);
}

private function onMapReady(event:MapEvent) {
setCenter(new LatLng(40.416740,-3.703250), 2, MapType.NORMAL_MAP_TYPE);
setZoom(2,false);
addControl(new ZoomControl());
addControl(new PositionControl());
addControl(new MapTypeControl());
}

}

Now we need to make a way for haXe to understand how to compile this with the API, so this is the contents of:
/usr/local/src/flashdevelopment/compile.hxml

-swf map.swf
-swf-version 9
-cp lib/hxclasses/
-swf-lib lib/library.swf
-main MyMap

Use haXe to compile this map into an SWF:

root@devbox:/usr/local/src/flashdevelopment$ haxe compile.hxml
root@devbox:/usr/local/src/flashdevelopment$

Voila! Your first Google Flash Map, fresh from the oven.. the filename is map.swf
Try to include this on a webpage, check the source here if you don’t know how to do it.
Your API key should be in the flashvars when applying the flash object on a webpage.
&ltparam name=”flashvars” value=”key=key”/&gt

Here is my result, I messed up the width/height of my app, but it works:

Hope someone can have use out of this! :)

Tags: , , ,

7 Websites with a good API for Mashups

May 22nd, 2009 by admin | 1 Comment | Filed in mashups

Mashups. To be able to think creatively about it you must know which sites offer an API. And then; what kind of data you can expect to be able to access. Here is a list of websites which offer an API. Some are the most popular, but you will find a little golden egg or two in this list.
fb_logosm

Facebook have an API, you can use most of the data users have stored at facebook, like friends, profile info, photos.
API Docs: Facebook API Documentation

twitter_logo
From Twitter you can get the user data, followers, friends and status updates. A previous article on this website was about Twitter applications.
API Docs:
Twitter API Documentation

lastfmlogo
With the last.fm API you can retrieve user data, most popular songs and everything stored within last.fm

A friend of mine created this mashup of Spotify and Last.fm.
API Docs: Last.Fm API Documentation

bitlylogo

With the bit.ly API you can expand and shorten URL’s, then gather statistics about traffic on those specific short URL’s.
A lot of Twitter users post links shortened with bit.ly, so it would also be possible to keep track of what’s popular by the use of this API.
API Docs: Bit.ly API Documentation


Create your own maps, you can use Google Maps API to display geodata in your mashup.
API Docs: Google Maps API Documentation

GeoNames
With the GeoNames API you can get data for your Google Maps; Airports, Hotels and a lot of other things. Trivial resource!
API Docs: GeoNames API Documentation

digglogo

With the Digg.com API you can get current popular stories from all categories, the digg count, comments and a lot of other data.
API Docs: Digg.com API Documentation

That’s the list!

Which other popular APIs exist? If you have used another API, leave a comment!

Tags: , , , , , , , ,