Thursday, June 21, 2012

Troubleshooting slapd error: too many open files

Debian testing comes with openldap (slapd package) version 2.4.28. I noticed that ldap clients start receiving error and cannot contact ldap service any more. While the slapd daemon was running I found a number of errors in syslog (file /var/log/syslog):
... ldap1 slapd[4894]: SASL [conn=1611] Failure: GSSAPI 
  Error: Unspecified GSS failure.  Minor code may 
  provide more information (Too many open files)
... ldap1 slapd[4894]: warning: cannot open 
  /etc/hosts.allow: Too many open files
... ldap1 slapd[4894]: warning: cannot open 
  /etc/hosts.deny: Too many open files
... ldap1 slapd[4894]: SASL [conn=1611] Failure: GSSAPI
  Error: Unspecified GSS failure.  Minor code may
  provide more information (Too many open files)
Take a look at slapd process max open files soft limit:
cat /proc/`pidof slapd`/limits
Check number of files open by slapd process:
pidof slapd | xargs lsof -a -p | wc -l
The slapd process must not exceed max open files soft limit. Take a look at files opened by slapd process:
pidof slapd | xargs lsof -a -p | tail
Here is a sample output that shows there are a number of deleted files... actually one file /var/tmp/ldap_103.
slapd   4894 openldap  117u   REG  0,197 3371 705975 
  /var/tmp/ldap_103 (deleted)
slapd   4894 openldap  118u   REG  0,197 3371 705975 
  /var/tmp/ldap_103 (deleted)
slapd   4894 openldap  119u   REG  0,197 3371 705975 
  /var/tmp/ldap_103 (deleted)
There is definitely a bug in slapd that cause max open files limit exceed.

Recycle Process

We can write a script that does a check and restart daemon if it reaches certain limit (file /usr/local/sbin/slapd-restart).
#!/bin/sh

# Restart slapd daemon if it has open more than 512 files
if [ `pidof slapd | xargs lsof -a -p | wc -l` -gt 512 ]
then
    /etc/init.d/slapd restart
fi
Let cron run this script hourly:
ln -s /usr/local/sbin/slapd-restart \
  /etc/cron.hourly/slapd-restart
If slapd process exceed max open files soft limit too quickly consider schedule cron job more frequently.

Mount /var/tmp in tmpfs

While slapd process creates small files in /var/tmp quite quickly I found reasonable to mount it with tmpfs (file /etc/fstab):
tmpfs /var/tmp tmpfs noatime,nodev,noexec,nosuid,size=1M 0 0
Restart slapd daemon so changes take place:
/etc/init.d/slapd stop
# Ensure the /var/tmp is empty
rm /var/tmp/ldap_103
mount /var/tmp
/etc/init.d/slapd start
# Ensure the /var/tmp mounted
df -h | grep /var/tmp
Ensure the /var/tmp mounted:
tmpfs           1.0M  4.0K 1020K   1% /var/tmp
Regularly take a look a /var/log/syslog if there are any errors reported.

Tuesday, June 12, 2012

How to Convert FLAC to MP3 in a Batch

Converting flac to mp3 package pre-requirements are the same as published in another post (How to Convert APE+CUE to MP3). So I will add what is necessary. You will need flac package.
apt-get -y install flac
Here is a script that does the rest (file flac-mp3.sh):
#!/bin/bash

for f in *.flac
do
    metaflac --export-tags-to=- "$f" | \
        sed 's/=\(.*\)/="\1"/' | \
        sed 's/\(.*\)=/\L&/' > tags.sh
    . ./tags.sh
    rm ./tags.sh
   
    out_dir="mp3/$artist/$album"
    if [ ! -d "$out_dir" ]; then
        mkdir -p "$out_dir"
    fi
    flac -cd "$f" | lame -h - -v --preset cd \
        --tt "$title" \
        --tn "$tracknumber" \
        --tg "$genre" \
        --ty "$date" \
        --ta "$artist" \
        --tl "$album" \
        --add-id3v2 \
        "$out_dir/${f%.*}.mp3"
done
Drop that file into a directory that has flac files. Run the script and in few minutes you will get a mp3 directory with your mp3 tracks folded by artist/album.

Thursday, June 7, 2012

Gnome Keyring: Location

Gnome keyring can automatically unlock passwords stored in the keyring. Gnome keyring include the following components: pkcs11, gpg, secrets, ssh. You can take a look at various passwords and keys stored by running (Alt + F2) seahorse.
Various keyrings are unlocked during user login. You can control which one by reviewing gnome startup application preferences, take a look by running gnome-session-properties.
The problem I faced with was related to the fact that keyring daemon place it runtime data into $HOME/.cache/keyring-* directory and over time there are quite a lot of them there. So while these data are session specific I would think it is most appropriate to store this information somewhere in temporary storage (e.g. /tmp) so it cleaned up. Fortunately you can define environment variable $XDG_RUNTIME_DIR that points to /tmp and that get it solved.
echo "export XDG_RUNTIME_DIR=/tmp" > \
    /etc/profile.d/gnome-keyring.sh
The keyring daemon properly manage file permission so it owned and readable by user only. Once you reboot your computer the system level profile will setup environment variable for you so keyring cache will be created out there.

Friday, June 1, 2012

Debian APC UPS client-server monitoring

Apcupsd is a UPS control system that permits orderly shutdown of your computer in the event of a power failure. We will take a look at NIS (Network Information Server) server and client configuration (this is the case when a single UPS powers several computers).

Server

NIS (Network Information Server) mode allows communication between different hosts. Only one of those hosts, the server, needs to talk to the UPS directly.
  1. Ensure device is connected and recognized. In most cases your UPS is connected to server via USB cable. In Linux you can check this by listing USB devices (provided by usbhid driver):
    deby1:~# ls /dev/usb/
    hiddev0
    
    If your device is no connected, most likely, you will get a message like this:
    ls: cannot access /dev/usb/: No such file or
    directory
    
    Note, in our case the UPS device is available at /dev/usb/hiddev0.
  2. Install UPS monitoring software. Apcupsd is a software designed to control APC UPS devices, let get it installed:
    apt-get install apcupsd
    
  3. Configure apcupsd. Ensure the following settings (file /etc/apcupsd/apcupsd.conf):
    UPSCABLE usb
    
    UPSTYPE  usb
    DEVICE   /dev/usb/hiddev0
    
    NISIP    0.0.0.0
    
    Let apcupsd daemon know it is configured (file /etc/default/apcupsd):
    ISCONFIGURED=yes
    
  4. Start apcupsd service:
    /etc/init.d/apcupsd start
    
    Check UPS status:
    apcaccess status <server name>
    
    Take a look at any errors reported (file /var/log/apcupsd.events):
    ...  apcupsd 3.14.10 (...) debian startup succeeded
    

Client

The client computer will communicate with server via network.
  1. Install UPS monitoring software.
    apt-get install apcupsd
    
  2. Configure apcupsd. Ensure the following settings (file /etc/apcupsd/apcupsd.conf):
    UPSCABLE ether
    
    UPSTYPE net
    #DEVICE  hostname:port
    DEVICE  deby1.dev.local:3551
    
    NETSERVER off
    
    Let apcupsd daemon know it is configured (file /etc/default/apcupsd):
    ISCONFIGURED=yes
    
  3. Start apcupsd service:
    /etc/init.d/apcupsd start
    

Notifications

You are able receive a number of notification events, e.g. power failure, etc (see a complete list of events here). By default apcupsd calls script located at /etc/apcupsd/apccontrol. This script echo some events to user console, as well as shuts down host per doshutdown event. You can easily extend this script to email you events. Here is the script (file /usr/local/sbin/notify.sh):
#!/bin/sh

domain=`hostname -d`
mail=root@$domain
msg=Test

if [ ! -z "$2" ]; then
    mail=$1; msg=$2
    if ! echo $mail | grep -q "$domain"; then
        mail=$mail@$domain
    fi
else
    if [ ! -z "$1" ]; then msg=$1; fi
fi

# strip whitespace at the end of message
msg=`echo "$msg" | sed 's/ *$//g'`

echo $msg | mail -s "$msg" $mail
echo $msg | wall
Ensure the following in apc event handler (file /etc/apcupsd/apccontrol):
#WALL=wall
WALL="xargs -0 notify.sh ups@dev.local"
This will email all events handled by apccontrol to ups@dev.local.

Friday, March 23, 2012

How to install PostgreSQL in Debian

PostgreSQL is an object-relational database management system.

Server

Installation in Debian is straight forward:
apt-get install postgresql
The installation adds a linux administrative user account postgres. You will need to set password (consider take a look how to generate a strong password here):
passwd postgres
There is also user postgres in database. The passwords for both should be different. Let change securely password for database user postgres (you will need this password to connect to database):
psql01:~# su - postgres
psql01:~$ psql 
psql (9.1.3)
Type "help" for help.

postgres=# \password 
Enter new password: 
Enter it again: 
postgres=# \q

Server Network Access

The installation configures the server to be available for local connections only. If you need this server to be accessible from other computers in your network follow these:
  • Ensure the server connection settings (file/etc/postgresql/9.1/main/postgresql.conf):
    # - Connection Settings -
    listen_addresses = '*'
    
  • Allow incoming network connections (file /etc/postgresql/9.1/main/pg_hba.conf):
    # Allow remote connections to any database, 
    # for any user from 192.168.10.0/24 network.
    host  all   all  192.168.10.0/24     md5
    
  • Restart server so your changes take place:
    /etc/init.d/postgresql restart
    
At this point you should get your PostgreSQL server installation finished.

Client

pgAdmin III is a database design and management application for use with PostgreSQL (graphical tool). Let install it:
apt-get install pgadmin3
Try connect to the server you installed with user postgres and password set for database user.