Display real time data via python / node.js / dojo using sockets

December 25th, 2013 No comments

This samples shows how you can show real time data in your modern browser using sockets. A python implementation gathers cpu statistics from the server and propagates them via unix sockets. Then a node.js unix socket client will pick the data up and share it via websockets with a client.

The python script looks as follows:

import time
import psutil
import subprocess
import textwrap
import json
import datetime
import socket
import os, os.path

class PyTop:
  def __init__(self, refresh):
    self.refresh = refresh
    self.cpuUsage()
    self.cpus()
    print "Init done"
  def cpus(self):
    percs = self.cpu_usage
    self.cpus = len(percs)
    #print self.cpus
  def cpuUsage(self):
    self.cpu_usage = psutil.cpu_percent(interval=0, percpu=True)
    #i = 1
    #for cpu in self.cpu_usage:
    #  print "CPU %d LOAD %d" % ( i, cpu)
    #  i += 1
  def topData(self):
    self.cpuUsage()
    self.data = {}
    self.data['cpus'] = self.cpus
    self.data['usage'] = self.cpu_usage
    self.data['now'] = datetime.datetime.now().isoformat()
  def output(self, interval):
    while True:
      self.topData()
      with open('../html/data.txt', 'w') as outfile:
        json.dump( self.data, outfile )
      time.sleep(interval)
      print self.data['now'] + " Updated File"
  def jsonData(self):
    self.topData()
    self.json = json.dumps(self.data, sort_keys=True, indent=4, separators=(',', ': ') )
    print self.json
  def toSocket(self, interval):
    sockfile = "./communicate.sock"
    if os.path.exists( sockfile ):
      os.remove( sockfile )
    print "Opening socket..."
    server = socket.socket( socket.AF_UNIX, socket.SOCK_STREAM )
    server.bind(sockfile)
    server.listen(5)
    print "Listening..."
    while True:
      conn, addr = server.accept()
      print 'accepted connection'
      while True:
        self.jsonData()
        #conn.write(self.json)
        conn.sendall(self.json)
        time.sleep(interval)
    print "-" * 20
    print "Shutting down..."
    server.close()
    os.remove( sockfile )
    print "Done"

pyTop = PyTop(0)
#pyTop.cpus()

pyTop.toSocket(1)
#pyTop.output(1)
#pyTop.json()

The node.js part looks as follows:

var static = require('node-static')
  , WebSocketServer = require('ws').Server
  , net = require('net')
  , log = require('npmlog')
  , sockfile = './communicate.sock';

var file = new static.Server('./public');

var server = require('http').createServer(function (request, response) {
    request.addListener('end', function () {
        file.serve(request, response);
    }).resume();
}).listen(8080);

var wss = new WebSocketServer({ server: server });
wss.on('connection', function(ws) {
  ws.on('message', function(message) {
    console.log('received: %s', message);
    wss.broadcast(message);
  });
    ws.send('something');
});

wss.broadcast = function(data) {
  for (var i in this.clients)
    this.clients[i].send(data);
};

var client = net.connect( { path: sockfile });

client
  .on('connect', function () {
    log.info('client', 'client connected');
    client.write('hello server');
    client.write('DONE');
  })
  .on('data', function (data) {
    log.info('client', 'Data: %s', data.toString());
    wss.broadcast(data.toString());
    client.end();
  })
  .on('error', function (err) {
    log.error('client', err);
  })
  .on('end', function () {
    log.info('client', 'client disconnected');
  });

This is the html file used:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Tutorial: Hello Dojo!</title>
</head>
<body>
<h1 id="greeting">Hello</h1>
<!-- load Dojo -->
<link rel="stylesheet" href="/dojo/dojo/../dijit/themes/claro/claro.css">

<script>dojoConfig = {parseOnLoad: true}</script>
<script src='/dojo/dojo/dojo.js'></script>

<script>
require([ "dojox/charting/Chart"
, "dojox/charting/axis2d/Default"
, "dojox/charting/plot2d/StackedAreas"
, "dojox/charting/themes/Wetland"
, "dojox/charting/plot2d/Bars"
, "dojo/ready"],
function(Chart, Default, StackedAreas, Wetland, Bars, ready){
ready(function(){
var c = new Chart("chartOne");
c.addPlot("default", { type: Bars, gap: 5, minBarSize: 1, maxBarSize: 100 } )
.addAxis("x")
.addAxis("y", { vertical: true, fixLower: "major", fixUpper: "major" })
.setTheme(Wetland)
.addSeries("Load", [0,0,0,0])
.render();
var myWebSocket = new WebSocket("ws://serverName:8080");
myWebSocket.onopen = function(evt) {
console.log("Connection open ...");
};
myWebSocket.onmessage = function(evt) {
var div = document.querySelector('div');
require(["dojo/json"], function(JSON){
var data = JSON.parse(evt.data, true);
c.updateSeries("Load", data.usage, false);
c.render();
console.log("data", data);
});
};
myWebSocket.onclose = function(evt) {
console.log("Connection closed.");
};
});
});
</script>

<div id="chartOne" style="width: 400px; height: 240px; margin: 10px auto 0px auto;"></div>

</body>
</html>
Categories: Web server Tags:

Setup USB DAC as primary device for ALSA

December 20th, 2013 No comments

First we can have a look at the sound cards that where recognized by alsa

cat /proc/asound/cards

This is the output on my machine:

bodo@devtop:~$ cat /proc/asound/cards
 0 [Intel          ]: HDA-Intel - HDA Intel
                      HDA Intel at 0xfc020000 irq 49
 1 [PCM2702        ]: USB-Audio - Burr-Brown Japan PCM2702
                      Burr-Brown Japan Burr-Brown Japan PCM2702 at usb-0000:00:1d.0-2, full speed
 4 [ThinkPadEC     ]: ThinkPad EC - ThinkPad Console Audio Control
                      ThinkPad Console Audio Control at EC reg 0x30, fw 7VHT12WW-1.01

I want to make the DAC with ID 1 default in place of device 0 and I would like to use alsa directly.
First step to reach this is to remove pulseaudio from the system. We can do this via the following command:

sudo apt-get --yes purge pulseaudio

We can also reset alsa with the following command

alsactl init

I now create a system wide configuration file for alsa as described .

sudo vim /etc/asound.conf

I added the following line to make the DAC the default device

pcm.!default {
        type hw
        card 1
}

ctl.!default {
        type hw
        card 1
}

Reboot to activate the changes.

We can now further optimize the settings in the asound file. You can use the following command to get more information about available devices.

sudo aplay -L

A more optimized version based on this would be

pcm.dmixer {
    type dmix
    ipc_key 1024
    ipc_key_add_uid false
    ipc_perm 0666                       # mixing for all users
    slave {
        pcm "hw:1,0"
        period_time 0
        period_size 1024
        buffer_size 8192
        rate 44100
    }
    bindings {
        0 0
        1 1
    }
}

pcm.dsp0 {
    type plug
    slave.pcm "dmixer"
}

pcm.!default {
    type plug
    slave.pcm "dmixer"
}

pcm.default {
   type plug
   slave.pcm "dmixer"
}

ctl.!default {
  type hw
  card 1
  device 0
}
Categories: alsa, dac, Linux, sound, Ubuntu, usb Tags:

Install shairport on Ubuntu

December 20th, 2013 No comments

First we can install Shairport, we can do this as described in the .

sudo apt-get install libssl-dev libavahi-client-dev libasound2-dev unzip make gcc openssl pkg-config

As described in we can now download the latest version of shairport.

In this sample we download to your home directory:

cd
wget https://github.com/abrasive/shairport/archive/1.0-dev.zip

Now we can compile shairport.

unzip 1.0-dev
cd 1.0-dev
./configure
make

In order to start shairport when the computer is booted we can add it to crontab.

sudo crontab -e

Add the following line at the bottom and replace YOURUSER with your user name

@reboot /home/YOURUSER/shairport-1.0-dev/shairport -a "UbuntuPlay" -d
Categories: Linux, sound, Ubuntu Tags:

PPTPD on Ubuntu

May 12th, 2013 No comments

Install pptpd:

apt-get install pptpd ufw

Enable ports in ufw:

ufw allow 22
ufw allow 1723
ufw enable

You might also want to enable ports of your other services. (http….)

Add a virtual network interface:

vim /etc/network/interfaces

Sample File:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
  address 10.0.0.10
  netmask 255.255.255.192
  gateway 10.0.0.129

# virtual interfaces

auto eth0:0
 iface eth0:0 inet static
  address 192.168.89.1
  netmask 255.255.255.0
  gateway 10.0.0.10
  dns-nameservers 10.0.0.12

Add your dns server to:

vim /etc/ppp/pptpd-options

Uncomment ms-dns and add your dns servers, this sample uses the public google dns servers:

ms-dns 8.8.8.8
ms-dns 8.8.4.4

Configure your vpn ip range:

vim /etc/pptpd.conf

Add the following to the end of the file:

localip 192.168.89.1
remoteip 192.168.89.2-20

Add some user account for the vpn server:

vim /etc/ppp/chap-secrets
# Secrets for authentication using CHAP
# client        server  secret                  IP addresses
user            pptpd   my_password             *

Uncomment the following lines in:

vim /etc/sysctl.conf
net.ipv4.ip_forward=1
net.ipv6.conf.all.forwarding=1

In:

vim /etc/default/ufw

Change the following line from:

DEFAULT_FORWARD_POLICY="DROP"

to:

DEFAULT_FORWARD_POLICY="ACCEPT"

In the following file:

vim /etc/ufw/before.rules

Add before the filter section:

# nat Table rules
*nat
:POSTROUTING ACCEPT [0:0]
# Allow forward traffic from eth0:0 to eth0
-A POSTROUTING -s 192.168.88.0/24 -o eth0 -j MASQUERADE

# dont delete the COMMIT line or these nat table rules wont be processed
COMMIT

Reboot to enable the new settings.

Categories: Ubuntu, Web server Tags:

Installing node.js on Ubuntu 12.04

October 21st, 2012 No comments

Yesterday I wanted finally to get my hands on node.js and I did some research about how to install it best on a Ubuntu server. I found the following very helpful post that actually got me started.

My first step was to create a new dedicated user for node.js.

useradd -g ftpuser -d /var/www/node -s /bin/ftp -m node
chown node.www-data /var/www/node
chmod 750 /var/www/node
mkdir /var/www/node/conf
mkdir -p /var/www/node/domain/{cgi-bin,exec,html,log,session,temp}
chmod 770 /var/www/node/domain/temp
chmod 777 /var/www/node/domain/session
chmod 700 /var/www/node/domain/exec
chown -R node.ftpuser /var/www/node/*
rm -rf /var/www/node/.b*
rm -rf /var/www/node/.p*

This follows the pattern for user creation established in previous posts.

After this I follow the steps described in the post mentioned above.

sudo apt-get update
sudo apt-get install build-essential -y

sudo apt-get install git -y

sudo apt-get install python libssl-dev -y

Now I install node into the home of the new user that we created before.

sudo su -s /bin/bash node
mkdir -p /var/www/node/local/src
cd /var/www/node/local/src
git clone git://github.com/joyent/node.git
cd node
sudo git checkout v0.8.12
./configure --prefix=/var/www/node/local
make
make install

Now that every thing is locally installed we make the binaries accessible.

echo 'PATH=$PATH:$HOME/local/bin' >> $HOME/.bashrc
source ~/.bashrc
Categories: git, Linux, Ubuntu Tags:

Create a custom linux-source-2.6.31 ubuntu 9.10 kernel

November 10th, 2009 2 comments

This is a quick and very dirty how to about building a custom kernel for ubuntu 9.10, it’s based on this how to.

First we make sure our system is up to date:

sudo apt-get update
sudo apt-get upgrade

Now we install all needed packages:

sudo apt-get install linux-source-2.6.31 kernel-package fakeroot kernel-wedge build-essential \
makedumpfile build-dep linux linux libncurses5 libncurses5-dev linux-headers-generic

We unpack the source files to a custom directory in our home directory:

mkdir ~/src
cd ~/src
tar xjvf /usr/src/linux-source-2.6.31.tar.bz2
cd linux-source-2.6.31

We want to reuse the existing config:

cp -vi /boot/config-`uname -r` .config

Now you can customize the kernel:

make menuconfig

Then we can build it:

make-kpkg clean
fakeroot make-kpkg --initrd --append-to-version=custom-1 kernel-image kernel-headers

Ensure that frame buffer drivers are loaded:

echo vesafb | sudo tee -a /etc/initramfs-tools/modules
echo fbcon | sudo tee -a /etc/initramfs-tools/modules

Install the new packages:

dpkg -i linux-image-2.6.31.4custom-1_2.6.31.4custom-1-10.00.Custom_i386.deb
dpkg -i linux-headers-2.6.31.4custom-1_2.6.31.4custom-1-10.00.Custom_i386.deb
Categories: Web server Tags:

Managing apache users on Ubuntu via shell scripts

October 17th, 2009 No comments

I created some shell scripts to add and delete apache web users. You can download them via this post.

This script creates users and requires this webserver setup:
addweb

You also need the following default files:
default-vhost
default-php5-fcgi-starte
php.ini

There is another script to delete users from apache and the system:
dellweb

Installation:

All files have to be saved in the same directory, to run the .sh files they must have proper rights:

chmod +x addweb.sh
chmod +x dellweb.sh

To run the scrips type:

 
sudo ./addweb username
sudo ./dellweb username

Then follow the dialog. On request the script could be writen dialog free as well.

Categories: Web server Tags:

Install postgresql on Ubuntu with php5

October 16th, 2009 No comments

In this installation we integrate postgresql into this apache installation.

First we refresh our package management and our packages:

apt-get update
apt-get upgrade

Then we have to install the basic packages:

apt-get install postgresql postgresql-server-dev-8.3 php5-pgsql

Now we have to do the basic setup of the postgresql server:

sudo -u postgres psql template1

Now you can define a passwort for the postgres user:

\password postgres

To leave the prompt enter:

\q
Categories: PHP, postgresql, Ubuntu Tags:

Install phpPgAdmin on Ubuntu

October 16th, 2009 No comments

In order to use this how to you have to install apache first following this how to. Also postgresql has to be installed, therefore you can follow this how to.

First we create a new user:

useradd -g ftpuser -d /var/www/pga -s /bin/ftp -m pga
chown pga.www-data /var/www/pga
chmod 750 /var/www/pga
mkdir /var/www/pga/conf
mkdir -p /var/www/pga/domain/{cgi-bin,exec,html,log,session,temp}
chmod 770 /var/www/pga/domain/temp
chmod 777 /var/www/pga/domain/session
chmod 700 /var/www/pga/domain/exec
chown -R pga.ftpuser /var/www/pga/*
rm -rf /var/www/pga/.b*
rm -rf /var/www/pga/.p*

Then we create a vHost for the new pga user:

cd /etc/apache2/sites-available/
vim pga

We add the following configuration:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        SuexecUserGroup pga ftpuser
        ServerName www.domain.tld
        ServerAlias more.domains.tld
        DocumentRoot /var/www/pga/domain/html
        DirectoryIndex index.php index.html index.htm

        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>

        <Directory />
                Order Deny,Allow
                Deny from All
        </Directory>

        <Directory /var/www/pga/domain/html>
                FCGIWrapper /var/www/php-fcgid-scripts/pga/php-fcgid-starter .php
                Options +ExecCGI
                order allow,deny
                allow from all
        </Directory>
        ErrorLog /var/www/pga/domain/log/apache_error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/www/pga/domain/log/apache_access.log combined
        ServerSignature On
</VirtualHost>

Now we create the fcgi configuration:

cd /var/www/php-fcgid-scripts/
mkdir pga
cd pga
touch php-fcgid-starter
chmod +x php-fcgid-starter
vim php-fcgid-starter

Now we add the configuration:

#!/bin/sh
PHPRC="/var/www/pga/conf"
export PHPRC
PHP_FCGI_CHILDREN=15
export PHP_FCGI_CHILDREN
exec /usr/bin/php5-cgi

We configure the proper rights:

chown -R pga.ftpuser /var/www/php-fcgid-scripts/pga
chmod 755 /var/www/php-fcgi-scripts/pga/php-fcgi-starter
chattr -V +i /var/www/php-fcgid-scripts/pga/php-fcgid-starter

We create a php.ini file for this vHost:

cd /var/www/pga/conf
wget http://www.naumann.cc/wp-content/uploads/2009/10/php.ini
chown root.root php.ini

Now we download the pgaPgAdmin interface:

cd /var/www/pga/domain/
su -m pga
wget http://downloads.sourceforge.net/project/phppgadmin/phpPgAdmin%20%5Bstable%5D/phpPgAdmin-4.2.2/phpPgAdmin-4.2.2.tar.bz2?use_mirror=mesh
tar xjvf phpPgAdmin-4.2.2.tar.bz2

Then we copy all files into the html directory:

mv phpPgAdmin-4.2.2/* html/

Now we have to configure the postgresql server for the usage of phpPgAdmin:

vim /etc/postgresql/8.3/main/postgresql.conf

Ennable the following line:

listen_addresses = 'localhost'

Create a new user for phpPgAdmin:

sudo -u postgres createuser -D -A -P myuser
sudo -u postgres createdb -O myuser mydb

Change the authentication settings in the following file to allow access via phpPgAdmin via this user:

vim /etc/postgresql/8.3/main/pg_hba.conf

Add this line:

local   all         myuser                             md5

before:

local   all         all                               ident sameuser
Categories: apache, PHP, postgresql, Ubuntu Tags:

Install a dyndns client on Ubuntu

October 16th, 2009 No comments

This how to will guide you through the process of installing a dynamic dns service client on Ubuntu.

First we update our package manager:

apt-get update
apt-get upgrade

Now that we have a current system we can install the new package:

apt-get install ddclient

Select your dyndns provider from the list, for me it’s www.dyndns.com.

Enter the domain name that should be updated into the following field.

Enter your user name and password.

In the next dialog you have to enter the network device whos ip where the domain name should be resolved to.

For me this is eth0.

You can change the settings also via the config file:

vim /etc/ddclient.conf

My file looks as follows:

# Configuration file for ddclient generated by debconf
#
# /etc/ddclient.conf

pid=/var/run/ddclient.pid
protocol=dyndns2
use=if, if=eth0
server=members.dyndns.org
login=username
password='password'
host.dyndns.biz
Categories: dyndns, Ubuntu Tags: