Reading logs with a Named Pipe

November 22, 2012 Leave a comment

Recently I had to write a piece of code that constantly would read a log file and if something wrong had happened a specific action should be taken.
For this reason I wrote this piece of code in Lua using Named Pipes.
If you are not familiar with Named Pipes check them out (from Wikepedia: A named pipe is system-persistent and exists beyond the life of the process and must be deleted once it is no longer being used.).

LOG_FILE = "/path/to/the/log/file"
LOG_PIPE = "/path/to/the/log_pipe"
LOG_STR  = "A word/phrase that we are searching for on the logs"

--- Create the logs pipe
function create_log_pipe()
    print("Creating the Logs' pipe...")

    local cmd_status = os.execute("rm -f " .. LOG_PIPE .. ";" ..
        "mkfifo " .. LOG_PIPE .. ";" ..
        "tail -n1 -f " .. LOG_FILE .." > " ..LOG_PIPE .. "&")

    if cmd_status == nil then
        print("[Error] Couldn't create the Logs' Pipe")
        exit
    else
        print("Logs' pipe was created")
    end
end

--- Open the logs' pipe
-- @return the logs' pipe descriptor
function open_log_pipe()
    print("Opening the Logs' pipe...")

    local logfd, _, _ = io.open(LOG_PIPE, "r")

    if not logfd then
        print("[Error] Couldn't open the Logs' pipe.")
        exit
    else
        print("Logs' pipe has opened successfully.")
    end

    return logfd
end

create_log_pipe()
log_pipe = open_log_pipe()

for line in log_pipe:lines() do
    repeat
        if string.find(line, LOG_STR) ~= nil then
            -- do something
        end
    until true
end

Credits on this thread.

Categories: Lua, Programming Tags: ,

Two 16bit integers that represent one 32bit float

April 24, 2012 3 comments

Recently I came across the following problem…

There was a function that could read from some device’s registers and return their values as uint16_t. The problem was that each two 16bit registers in sequence (reg[0] and reg[1], reg[2] and reg[3], etc) were storing a 32bit float number (in little endian).

The first thing that came into my mind was to concatenate the data

integer_32 = ((integer_16_2 & 0xFFFF) << 16) | (integer_16_1 & 0xFFFF)

and produce a 32bit integer. Then this 32bit integer would be casted to a float.

Nevertheless, this is not correct because the bits of the registers were representing a float and not an integer. So if we produced lets say the integer_32 “3” the float that would be generated would be “3.0” and not the float that is represented by the binary “11”.

The solution to this problem was the following:

#include <string.h>

/**
 * Reform two 16bit unsigned integers that store one 32bit
 * signed float (little endian).
 * @param u1 the first 16bit unsigned
 * @param u2 the second 16bit unsigned
 * return the 32bit float
 */
float reform_uint16_2_float32(uint16_t u1, uint16_t u2)
{
    long int num = ((u2 & 0xFFFF) << 16) | (u1 & 0xFFFF);
    float numf;
    memcpy(&numf, &num, 4);
    return numf;
}
Categories: Programming Tags:

Why do we need cars if we have bicycles?

February 4, 2012 Leave a comment

I don’t know if this story is true, but someone (person, blog, … I don’t remember who) had told me that when the first car was build the other people said that it is useless since there are bicycles. They would never need a car since the most places that they had to transport themselves to, were near enough to buy such an expensive machine.

The same with personal computers, cable phones, mobile phones, internet, open source, electronic money, e-commerce, wikipedia, blogs, online social networks, online activism etc.

I remember to tell when Mercedes had released the first self-parking car, that this is too expensive for such a simple task. But now we have self-driving cars from Google. This is the way research works.

So when you see a product like a washing machine that connects itself on the Internet, don’t tell that this is useless. Just picture a better future. The future of the Internet of Things. Yes it is here and I know that because I am working on that!

Categories: Internet Of Things Tags:

Static IP on OpenWrt

January 23, 2012 Leave a comment

If you want your router to assign a static IP on a specific OpenWrt Machice you just have to run something like the following:

uci set network.lan.proto=static
uci set network.lan.ipaddr=192.168.1.130
uci set network.lan.netmask=255.255.255.0
uci set network.lan.gateway=192.168.1.1
uci set network.lan.dns=8.8.8.8

/etc/init.d/network restart

Also you can edit accordingly the /etc/config/network file.

Categories: OpenWrt, Tutorials

Backup your Gmail account with getmail

December 29, 2011 1 comment

For almost a year now I am using Gmail as a client to access all my mail accounts. In this article I am going to describe how to make backups of you Gmail account with getmail. Getmail is a simple, yet powerful console tool that downloads your mails and of course it is written in Python. Also because it makes a backup of the mails in an mbox-formatted file, you can access them with many mail clients (like Thunderbird) that support this format.

After installing it (for Debian: “sudo apt-get install getmail4″) you just have to create a file named “~/.getmail/getmail.gmail” that will contain the followings:

[retriever]
type = SimplePOP3SSLRetriever
server = pop.gmail.com
username = yourname@gmail.com
password = yourpassword
[destination]
type = Mboxrd
path = ~/gmail-archive/gmail-backup.mbox
[options]
verbose = 2
message_log = ~/.getmail/gmail.log

Here you can find some other examples on how to write the above configuration file.

Also you have to create the empty mbox file, “~/gmail-archive/gmail-backup.mbox”, that will contain your backed-up mails.

Then, go to the Gmail’s settings and select from the “Forwarding and POP/IMAP” tab, “Enable POP for all mail (even mail that’s already been downloaded)”. Also make sure to select for the “When messages are accessed with POP”, “keep Gmail’s copy in the inbox”.

Now you are ready to backup your Gmail account.
In order to do that, run the following command in the terminal:

getmail -r ~/.getmail/getmail.gmail

You should see a long string of messages begin printing out as getmail starts grabbing the contents of your Gmail account.
Because Google has some limits on how many messages can be retrieved at one time you will have to run the command several times to retrieve all the mails.
Or just run something like the following:

#!/bin/bash
# run the getmail command 1000 times
for i in `seq 1 1000`;
do
    getmail -r ~/.getmail/getmail.gmail
done

It would be useful to auto-backup your mails periodically. Personally, I made a cronjob that checks for new mails hourly.
To do so, run:

crontab -e

And at the end of the document, add the following line:

@hourly /usr/bin/getmail -r path-to-your-home-folder/.getmail/getmail.gmail

See also:
Why getmail and not fetchmail?
Gmail silently destroys mail

Credits to:
http://howto.wired.com/wiki/Make_a_Local_Backup_Of_Your_Gmail_Account

Categories: Linux

Περί ορέξεως… μπάμιες!

December 13, 2011 Leave a comment

Όσοι με ξέρουν προσωπικά, γνωρίζουν ότι όταν αρχίζω και μιλάω σχετικά με πολιτική, παραγωγικότητα, επιχειρηματικότητα και Open Source παθιάζομαι! Πριν τρεις βδομάδες λοιπόν αποφάσισα να εγκαταλείψω την ενασχόληση μου με την πολιτική, με τον τρόπο που τα media μας την επιβάλουν. Η πολιτική αυτή σεναριολογία απλά μου έκανε κακό στα άλλα τρία και γενικότερα σε εμένα σαν νέο άνθρωπο με στόχους και φιλοδοξίες.

Παρόλα αυτά, τυχαία σήμερα έπεσα για πέντε λεπτά πάνω στους Νέους Φακέλους. Ε… αυτό ήταν! Μέσα σε πέντε λεπτά ήμουν έτοιμος να σπάσω τα πάντα.

Έχω βαρεθεί να ακούω συνέχεια βλάκες να λένε βλακείες. Τα προβλήματα μας είναι γνωστά σε όλους μας δεκάδες χρόνια τώρα. Ας ξεκινήσουμε λοιπόν από αυτά. Ας προσπαθήσουμε να τα λύσουμε για μία φορά και μετά αν δε τα καταφέρουμε ας πάμε και σε άλλα θέματα όπως δραχμή κλπ κλπ. Βλέπω όλους αυτούς τους καθηγητές μεγάλων Πανεπιστημίων, και το μόνο που κάνουν μαζί με τους πολιτικούς είναι σεναριολογίες γιατί βαριούνται και δε θέλουν να λύσουν τα προβλήματα.

Κύριοι μου είσαστε απλά άχρηστοι για να διοικείτε χώρες, για αυτό μη μιλάτε. Όσον αφορά τους πολιτικούς, φταίει ο Ελληνικός λαός. Όσον αφορά όμως τους καθηγητές, τα media που τους βγάζουν. Οι καθηγητές είναι καλοί στην έρευνα τους, όχι να διοικούν. Για αυτό είναι καθηγητές. Αυτό δε το λέω υποτιμητικά. Ίσα ίσα, τους πραγματικούς ερευνητές (== οραματιστές του αύριο) τους θαυμάζω περισσότερο από όλους. Δεν είμαι σαν αυτούς που πιστεύουν ότι το να στέλνουμε robot στον Άρη είναι κάτι άχρηστο και κακό ενώ πεινάει ο κόσμος. Όσοι το λένε, μάλλον δεν έχουν περάσει ποτέ από κάποιο Πανεπιστήμιο ή απλά νομίζουν ότι πέρασαν.

Επειδή δεν θέλω να γράψω κατεβατό, έχω να πω μόνο ένα πράγμα. Ας αρχίσουμε, μόνος του ο καθένας μας, να κάνει σωστά την δουλειά του, προσπαθώντας να μην κάνει κακό στον άλλο, και όλα θα πάνε καλύτερα σε ατομικό επίπεδο. Για το συλλογικό έχουμε δουλειά ακόμα. Η πρώτη αρχή θα είναι να εκλέξουμε για μια φορά και κανέναν χρήσιμο μέσα σε αυτή τη Βουλή.

Α, και αυτό περί ελευθερίας έκφρασης της άποψης… Ναι πολύ καλό, αλλά όχι όταν μας πασάρεται τον κάθε βλάκα να λέει την βλακεία του για πράγματα που δεν έχει σχέση. Αν αυτοί οι κύριοι που βγαίνουν στα κανάλια ξέραν ότι μέσα από αυτό βαθμολογούνταν και κρινόταν η καριέρα τους, δε πιστεύω να τόλμαγαν απλά να κάνουν σενάρια και να κινδυνολογούν.

Αν έχετε χρόνο τσεκάρετε και αυτό: Working Hours and Productivity in EU

Categories: Generalities

Choose what really matters

December 11, 2011 Leave a comment

Immortal Technique is one of my best MCs along with FF.C and Active Member.

Not that I agree with everything that they say, but you must agree that they have clear view of the world and what they are doing.

In one song of his, he tells the followings:

There is a market for everything man
There is a market for pet psychologists nigga.
There is a market for twisted shitfetish video’s.
For nipplerings, for riverdancing, for chocolate cupboard roaches.
But you can’t find one for cultured hardcore reality and hiphop?

I want to think about that when I am thinking about healthy entrepreneurship.

So I must add to the above:
Don’t sell yourself to be what the others want you to be. To be successful like the global market and the status quo commands.
The life that your father and mother gave you as a present was in order to enjoy it and to spend it by doing something worthwhile for you and the world that you live in.
And yes you can survive by doing this.

Fight for what matters to you. It will be difficult but it will be worthwhile.

Categories: Generalities
Follow

Get every new post delivered to your Inbox.