Linux Cheat Sheet

PROCESS BASICS

ps auxww -H

All processes, with params + hierarchy

pgrep -fl $PROCESS_NAME

Filtriraj procese

strace -f -p $PID

What is a process doing?

lsof -p $PID

What files does a process have open?

watch 'ps aux | grep $PROCESS_NAME'

Keep an eye on a process

MEMORY

free -m
cat /proc/meminfo

How much mem is free?

vmstat 1

Are we swapping?

ps aux --sort=-resident|head -11

List the top 10 memory hogs

DATABASES

pt-query-digest --processlist h=localhost --print --no-report --user xxxx --password *****

“Tail” all queries hitting mysql. More

ssh -L 3307:localhost:3306 user@hostname -N

Connect to production mysql locally on port 3307 via ssh

DISK/FILES

iostat -xnk 5

Check reads/writes per disk

find . -size +100M

Find files over 100MB

find . -mtime -7

Find files created within the last 7 days

find . -mtime +14 -type f -name '*.gz'

Find files older than 14 days

find *.gz -mtime +14 -type f -exec rm {} \;

Delete files older than 14 days

tail -f file.log | grep 192.168.1.1

Monitor a log file for an IP or anything else

NETWORK

lsof -nPi tcp

TCP sockets in use

ip addr
ifconfig
iwconfig

Get IP/Ethernet info

mtr google.com

Traceroute with stats over time (top for traceroute)

tcptraceroute google.com

Traceroute using TCP to avoid ICMP blockage

iftop

Show traffic by port

netstat -tlnp

Show all ports listening with process PID

Izvor: http://rubytune.com/cheat

Django south

South je aplikacija za Django koja prati promjene Django Modela i zapisuje te promjene u bazu.
npr. model Korisnik ima polja: Ime i Prezime dužine 10 znakova.
Kasnije se pokaže da je to pre malo znakova.
Bez south aplikacije bi morali ručno promjeniti polja u bazi.
South nam omogućava da promjene vršimo direktno u modelima.

$ easy_install south

Naredba za instalaciju
Moramo dodati ‘south’ u INSTALLED_APPS u settings.py

$ python.py manage.py schemamigration app_name --initial

Napravimo inicijalnu migraciju, tj. snimimo postojeće stanje.

$ python.py manage.py syncdb

Moramo pokrenuti ‘syncdb’ jer south dodaje svoje tablice u bazu.
Redoslijed je bitan, jer ako pokrenemo syncdb prije schemamigration, syncdb ce zapisati tablice u bazu i south neće moći kreirati tablice, jer već postoje..

$ python.py manage.py migrate app_name

Naredba koja zapiše promjene modela u bazu. (Kreira ih, modificira, briše…)

$ python.py manage.py schemamigration app_name --auto

Nakon što napravimo promjene u Modelu pokrećemo south koji detektira razlike i zapisuje ih.

Validation

// Provjeri da li je vrijednost nešto smisleno, npr da li je array pun
if (typeof array[index] !== 'undefined' && array[index] !== null) { }

/* VALIDATION */

/*
 * If the value is null and not required, we don't need to run through validation
 */
if (!field.value || field.value === '' || typeof field.value === 'undefined') {
    return;
}
// Regex za validation !!
var ruleRegex = /^(.+)\[(.+)\]$/,
        numericRegex = /^[0-9]+$/,
        integerRegex = /^\-?[0-9]+$/,
        decimalRegex = /^\-?[0-9]*\.?[0-9]+$/,
        emailRegex = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,6}$/i,
        alphaRegex = /^[a-z]+$/i,
        alphaNumericRegex = /^[a-z0-9]+$/i,
        alphaDashRegex = /^[a-z0-9_-]+$/i,
        naturalRegex = /^[0-9]+$/i,
        naturalNoZeroRegex = /^[1-9][0-9]*$/i,
        ipRegex = /^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})$/i,
        base64Regex = /[^a-zA-Z0-9\/\+=]/i;
// Koristi se: npr.:   emailRegex.test('tvlahovi@gmail.com')    -> Vraca true ili false

Django WSGI Apache

Kreirati projekt.com.conf i prebaciti ga u /etc/httpd/conf.d

Konfiguracija:

Listen 8000

WSGISocketPrefix /var/run/wsgi


ServerName gandalf.local

Alias /static /vault0/podaci/_razvoj/webapps/is42/static
Alias /admin_media  /home/myuser/Django-1.1/django/contrib/admin/media

WSGIDaemonProcess is.dizajnzona42.com display-name=%{GROUP} python-path=/vault0/podaci/_razvoj/webapps/is42:/vault0/podaci/_razvoj/webapps/venv/is.dizajnzona42.com/lib/python2.7/site-packages
WSGIProcessGroup is.dizajnzona42.com

WSGIScriptAlias / /vault0/podaci/_razvoj/webapps/is42/is42/wsgi.py


    
        Order deny,allow
        Allow from all