Pages

Friday, October 18, 2013

Importing CSV file into django model.

Lets assume that we have the following django model:
class Person(models.Model):
    PersonID = models.CharField(max_length=10)
    FirstName = models.CharField(max_length=30)
    LastName = models.CharField(max_length=30)
    Address = models.CharField(max_length=30)

and we have a csv file with 4 elements on each row and we want to import them in our django database. Here is example (delimeter is !):
1!Nikolay!Hristov!Bulgaria, Gabrovo, Test street 18 
What we need to do is to make a view and attach it to certain url (for example http://localhost/import_db/). Here is the view:
def import_db(request):
   
    f = open('/path/to/filename-with-data.csv', 'r'
    for line in f:
        line =  line.split('!')
        tmp = Person.objects.create()
        tmp.PersonID = line[0]
        tmp.FirstName = line[1]
        tmp.LastName = line[2]
        tmp.Address = line[3]
        tmp.save()

    f.close()
Now all we have to do is to point our browser to http://localhost/import_db/ and wait for data to be imported.

Wednesday, September 18, 2013

Qmail starting error "alert: cannot start: unable to open mutex"

If you are getting this error message when you try to start qmail-send - most probably you are moving qmail from old installation to a new server.

The problem comes from missing diretories (and files) which are on most distributions links and when you archive it the real directories will not be trasfered.

Directory is /var/qmail/queue and as you can see from the example bellow it is just a symlink to another directory (example is from NetBSD qmail package):

 # ls -la /var/qmail      
drwxr-xr-x   2 root  wheel  512 Sep 18 15:25 .
drwxr-xr-x  26 root  wheel  512 Aug 24 22:11 ..
lrwxr-xr-x   1 root  wheel   24 Jul 16 04:36 alias -> /usr/pkg/etc/qmail/alias
lrwxr-xr-x   1 root  wheel   12 Jul 16 04:36 bin -> /usr/pkg/bin
lrwxr-xr-x   1 root  wheel   34 Jul 16 04:36 boot -> /usr/pkg/share/examples/qmail/boot
lrwxr-xr-x   1 root  wheel   26 Jul 16 04:36 control -> /usr/pkg/etc/qmail/control
lrwxr-xr-x   1 root  wheel   24 Jul 16 04:36 doc -> /usr/pkg/share/doc/qmail
lrwxr-xr-x   1 root  wheel   12 Jul 16 04:36 man -> /usr/pkg/man
lrwxr-xr-x   1 root  wheel   16 Jul 16 04:36 queue -> /var/spool/qmail
lrwxr-xr-x   1 root  wheel   24 Jul 16 04:36 users -> /usr/pkg/etc/qmail/users

So the real directory is "queue -> /var/spool/qmail" and here is the listing of subdirs:

# ls -la
drwxr-xr-x  11 qmailq  qmail  512 Sep 18 15:34 .
drwxr-xr-x   8 root    wheel  512 Jul 16 04:36 ..
drwx------   2 qmails  qmail  512 Sep  9 15:11 bounce
drwx------  25 qmails  qmail  512 Jan 19  2008 info
drwx------   2 qmailq  qmail  512 Sep 18 13:55 intd
drwx------  25 qmails  qmail  512 Jan 19  2008 local
drwxr-x---   2 qmailq  qmail  512 Jan 19  2008 lock
drwxr-x---  25 qmailq  qmail  512 Jan 19  2008 mess
drwx------   2 qmailq  qmail  512 Sep 18 13:55 pid
drwx------  25 qmails  qmail  512 Jan 19  2008 remote
drwxr-x---   2 qmailq  qmail  512 Sep 18 13:55 todo


Make sure to transfer this directory to the new server and also chown the dirs with correct permissions.

The actual error is caused by missing file sendmutex which resides in /var/spool/qmail/lock/ directory

# ls -la /var/spool/qmail/lock
total 12
drwxr-x---   2 qmailq  qmail   512 Jan 19  2008 .
drwxr-xr-x  11 qmailq  qmail   512 Sep 18 15:34 ..
-rw-------   1 qmails  qmail     0 Jan 19  2008 sendmutex
-rw-r--r--   1 qmailr  qmail  1024 Sep 16 09:39 tcpto


If this file does not exist just create it with the following command (make sure also to change the file owner):
# touch /var/qmail/queue/lock/sendmutex
# chown qmails:qmail /var/qmail/queue/lock/sendmutex


Saturday, September 7, 2013

ТУ Габрово, КСТ, задача 7, страница 79

КСТ, Задача (страница 79, задача 7):

Да се създаде програма, която въвежда символен низ. Да се преобразуват буквите му: от малки в големи и от големи в малки. За целта могат да се използват функциите isupper, islower, toupper, tolower от ctype.h

Таблица на съответствията

Наименование
Описание
broiach
Целочислена променлива, използвана за брояч на цикъл
buf(1024)
Буфер за символният низ със големина 1024 байта
p
Указател, нужен за функцията strchr()
tmp
Временна променлива тип «символ» (char)


Source Code на решението на задачата:

/* Author: Nikolay Hristov, 28.08.2013
 * Използвана литература:
 * - NetBSD Library Functions Manual, NetBSD 4.0, December 24, 2004
 * - Програмният език C, второ издание, Brian Kernigham, Dennis Richie,
 * издателство ZeST Pres, 2004 */

#include <stdio.h>
/* strchr() */
#include <string.h>
/* isupper(), islower(), toupper(), tolower() */
#include <ctype.h>

int main(void){

    int broiach;     /* ... */
    char buf[1024];    /* буфер */
    char *p;     /* указател, нужен за функцията strchr() */
    char tmp;    /* временна променлива тип char, нужен за isupper()/islower() */

    printf("Въведете текст за конвертиране: ");
    /* четем от стандартния вход (stdin) символен низ със размер
     * buf в случая 1024 bytes */
    while(fgets(buf, sizeof(buf), stdin)){
        /* Функцията strchr връща указател към търсения от нас символ.
         * Търсим символа 'нов ред' ('\n') и ако не открие такъв - връща NULL.
         * Ако не открие 'нов ред' в низа, това означава, че е въведен
         * низ > размера на буфера buf (в случая 1024 bytes), което означава,
         * че е натиснат Enter след края на буфера (въведени са минимум 
         * 1025 символа. */
        if((p = strchr(buf, '\n')) == NULL){
            printf("Въведен е прекалено дълъг низ.\n");
            return 1;
        }

        /* При откриване на съвпадение с 'нов ред' ('\n'),
         * функцията strchr връща указател към тази част от масива.
         * Задължително терминираме низа с s \0 */
        
        *p = '\0';
        
        /* Отпечатваме орогиналният низ */
        printf("Въведен низ    : %s\n", buf);
        
        /* Инициализираме броячът  */
        broiach = 0;

        /* Цикъл, който обхожда целия низ, докато стигне \0 */    
        while(buf[broiach]){
            /* Тък като islower/isupper работят само с char, 
             * обхождаме целия низ символ по символ */
            tmp = buf[broiach];

            /* Ако символа е с малки букви ... */
            if(islower(tmp)){
                /* правим ги на ГОЛЕМИ БУКВИ. */
                buf[broiach] = toupper(buf[broiach]);
            }

            /* Ако символа е с ГОЛЕМИ БУКВИ ... */
            if(isupper(tmp)){
                /* ПРАВИМ ГИ НА малки букви. */
                buf[broiach] = tolower(buf[broiach]);
            }

            /* Отиваме на следващия символ */
            broiach++;
        }

        /* Отпечатваме конвертирания текст */
        printf("Конвертиран низ: %s\n", buf);
        /* Очакваме ново въвеждане */
        printf("Въведете текст за конвертиране: ");
    }
}

Monday, July 22, 2013

NetBSD 6.1 filesystem notes.

The filesystem must be not mounted
root@farnsworth ~ # umount /disks/disk1
root@farnsworth ~ # umount /disks/disk2
root@farnsworth ~ # file -s /dev/wd1a
/dev/wd1a: Unix Fast File system [v1] (little-endian), last mounted on /disks/disk1, last written at Tue Jul 16 09:38:49 2013, clean flag 1, number of blocks 122096646, number of data blocks 121138363, number of cylinder groups 644, block size 32768, fragment size 4096, minimum percentage of free blocks 5, rotational delay 0ms, disk rotational speed 60rps, TIME optimization
root@farnsworth ~ # file -s /dev/wd2e
/dev/wd2e: Unix Fast File system [v1] (little-endian), last mounted on /disks/disk2, last written at Sat Jul 20 02:00:58 2013, clean flag 1, number of blocks 78142160, number of data blocks 77529094, number of cylinder groups 412, block size 32768, fragment size 4096, minimum percentage of free blocks 5, rotational delay 0ms, disk rotational speed 60rps, TIME optimization
root@farnsworth ~ #
Also you can do it with dumpfs:
root@farnsworth ~ # dumpfs -s /dev/wd1a
file system: /dev/rwd1a
format  FFSv1
endian  little-endian
magic   11954           time    Sat Jul 20 02:21:25 2013
superblock location     8192    id      [ 51e514a9 69cdbabc ]
cylgrp  dynamic inodes  4.4BSD  sblock  FFSv2   fslevel 4
nbfree  15140244        ndir    1       nifree  30334972        nffree  18
ncg     644     size    122096646       blocks  121138363
bsize   32768   shift   15      mask    0xffff8000
fsize   4096    shift   12      mask    0xfffff000
frag    8       shift   3       fsbtodb 3
bpg     23699   fpg     189592  ipg     47104
minfree 5%      optim   time    maxcontig 2     maxbpg  8192
symlinklen 60   contigsumsize 2
maxfilesize 0x004002001005ffff
nindir  8192    inopb   256
avgfilesize 16384       avgfpdir 64
sblkno  8       cblkno  16      iblkno  24      dblkno  1496
sbsize  4096    cgsize  32768
csaddr  1496    cssize  12288
cgrotor 0       fmod    0       ronly   0       clean   0x02
wapbl version 0x1       location 2      flags 0x0
wapbl loc0 488401024    loc1 131072     loc2 512        loc3 3
flags   wapbl
fsmnt   /disks/disk1
volname         swuid   0
Differences between FFSv1 and FFSv2

From newfs man page -O option:
0    4.3BSD; This option is primarily used to build root file systems that can be understood by older boot ROMs.  This generates an FFSv1 file system with level 1 format.
1    FFSv1; normal Fast File System, level 4 format. Also known as `FFS', `UFS', or `UFS1'.  This is the default.
2    FFSv2; enhanced Fast File System, suited for more than 1 Terabyte capacity.  This is also known as `UFS2'.

from fsck_ffs man page:

FFS1 level 0 = inode 4.2/4.3BSD static table
FFS1 level 1 = dynamic table
FFS1 level 2 = 32bit UID/GID, compact symlinks
FFS1 level 3 = free segment maps
FFS1 level 4 = FFS2 style superblock (allows WAPBL)
FFS2 level 5 = 64bit addresses, 64bit timestamps, birthtime, ext attributes 
FFSv2 is suitable for large disk (1TB)

Here is example how to change default percentage of disk space held back from normal users to 1%. Default is 5% and this is too much for large disks.
root@farnsworth ~ # tunefs -m 1 /dev/wd1a
tunefs: tuning /dev/rwd1a
tunefs: minimum percentage of free space changes from 5% to 1%
tunefs: should optimize for space with minfree < 5%


Monday, July 1, 2013

Lotus Domino 8.5 on Linux causing 100% CPU load.


This is known bug with platform statistics thread which is caught in infinite loop state. Solution is to modify your notes.ini file and disable platform stats:

PLATFORM_STATISTICS_DISABLED=1

Restart your server and there will be no more 100% cpu load.

Thursday, June 20, 2013

Using GDM and XDMCP with remote client

How to run Xorg session on client machine.

On the server (IP 10.10.10.1) by default GDM remote connect via XDMCP is disabled. We need to enable it. Configuration file is /etc/gdm/custom.conf (on RHEL/Oracle Linux) and /etc/gdm3/daemon.conf (on Debian). Example is from Oracle Linux 6.4:
# cat /etc/gdm/custom.conf
# GDM configuration storage

[daemon]

[security]

[xdmcp]

[greeter]

[chooser]

[debug]
Add these lines:
# cat /etc/gdm/custom.conf
# GDM configuration storage

[daemon]

[security]
DisallowTCP=false

[xdmcp]
Enable=true

[greeter]

[chooser]

[debug]
GDM must be restarted for this to work.

On client machine start the Xorg server like this (assuming that you have already X running on :0):
# X :1 -query 10.10.10.1
This will bring GDM to your display at display :1 and now you can login and work on client machine as you are logged directly to the server and switch between local and remote X with Alt + Fxx.

Monday, June 3, 2013

Roundcube missing contacts after changing IP address of the server.

If you have working installation of Roundcube and for some reason change your IP address the result is missing address book entries of all users.

If in the configuration file config/main.inc.php the line:
$rcmail_config['default_host'] = '10.10.200.1';
is changed to some other IP address (for instance 192.168.10.1) the result is missing address book entries.

In fact entries are not missing but Rouncube shows only enties for the users with current 'default_host' IP address. This means that you have two entries for each users with different 'default_host' field. How to fix it?

First delete all newly created entries with the new default_host:
old: 10.10.200.1, new: 192.168.10.1.
# mysql -u roundcubeuser -proundcubepass
mysql> use roundcube
mysql> delete from users where mail_host='192.168.10.1';
Query OK, 60 rows affected (0.07 sec)
Now change all remained entries with the new default_host:
mysql> update users set mail_host='192.168.10.1' where mail_host='10.10.200.1';
Query OK, 105 rows affected (0.08 sec)
Rows matched: 105  Changed: 105  Warnings: 0
Now you have your addressbook back.