среда, 17 июля 2013 г.

Python: set network interface for urllib2

The problem is that urllib2 is based on httplib library which doesn't have such functionality. The best solution I found was this “monkey patch” from Alex Martelli posted on Stack Overflow:
import socket
true_socket = socket.socket
def bound_socket(*a, **k):
    sock = true_socket(*a, **k)
    sock.bind((sourceIP, 0))
    return sock
socket.socket = bound_socket
Simply run this block of code before using urllib2 functions.

пятница, 5 июля 2013 г.

Zabbix + Nginx + PostgresDB

Assuming you already have running nginx and postgres and are going to install Zabbix server. Download and install Zabbix. Then you'll need to install FastCGI for PHP support and several additional packages:
# apt-get install zabbix-server-pgsql zabbix-frontend-php \
    php5-pgsql php5-fpm
You should be prompted to configure postgres database for zabbix during the installation. If not, refer to the documentation on how to do it manually.

Lets proceed to adding PHP support to Nginx for Zabbix. Add the following block to your 'server' in nginx.conf:
location /zabbix {
    if ($scheme ~ ^http:){
        rewrite ^(.*)$  https://$host$1 permanent;
    }
    
    alias               /usr/share;
    index               index.php;
    error_page          403 404 502 503 504  /zabbix/index.php;

    location ~ \.php$ {
        if (!-f $request_filename) { return 404; }
        expires         epoch;
        include         /usr/local/nginx/conf/fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_index   index.php;
        fastcgi_pass    127.0.0.1:9000;
    }

    location ~ \.(jpg|jpeg|gif|png|ico)$ {
        access_log      off;
        expires         33d;
    }
}
Modify 'include' directive to point to your nginx configuration directory and 'alias' - to zabbix installation dir. Note that if zabbix is installed into
/usr/share/zabbix
set alias to
/usr/share
Now set fastcgi to run on port 9000 and not using socket. Modify
/etc/php5/fpm/pool.d/www.conf
and set:
listen = 127.0.0.1:9000
Now Zabbix page can be opened in your browser: http://<server_ip_or_name>/zabbix. In case it doesn't check all the settings above and nginx and fastcgi logs to fix the problem. Once done proceed with installation steps on the opened page.