2012年1月17日 星期二

Using iptables to create nat

Internet setting:
Interface: eth0
IP: 172.16.116.1
MASK: 255.255.0.0
GATEWAY: 172.16.1.254

Nat setting:
Interface: eth1
IP: 192.168.116.1
MASK: 255.255.255.0
GATEWAY: 192.168.116.1

Content of /etc/network/interface:
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
    address 172.16.116.1
    netmask 255.255.0.0
    gateway 172.16.1.254
    # Enable package forwarding
    up sudo sh -c 'echo "1" > /proc/sys/net/ipv4/ip_forward'
    up sudo iptables -t nat -A POSTROUTING -j MASQUERADE -o $IFACE
    # Disable package forwarding
    down sudo sh -c 'echo "0" > /proc/sys/net/ipv4/ip_forward'
    down sudo iptables -t nat -D POSTROUTING -j MASQUERADE -o $IFACE

auto eth1
iface eth1 inet static
    address 192.168.116.1
    netmask 255.255.255.0
    # Enable package forwarding
    up sudo sh -c 'echo "1" > /proc/sys/net/ipv4/ip_forward'
    #up sudo iptables -A POSTROUTING -s 192.168.116.0/24 -o eth0 -j SNAT --to 172.16.116.1
    # Disable package forwarding
    down sudo sh -c 'echo "0" > /proc/sys/net/ipv4/ip_forward'
    #down sudo iptables -D POSTROUTING -s 192.168.116.0/24 -o eth0 -j SNAT --to 172.16.116.1

2012年1月16日 星期一

Ubuntu preseed file by Dustin Kirkland

# Ubuntu Server Quick Install
# by Dustin Kirkland 
#  * Documentation: http://bit.ly/uquick-doc

d-i     debian-installer/locale string en_US.UTF-8
d-i     debian-installer/splash boolean false
d-i     console-setup/ask_detect        boolean false
d-i     console-setup/layoutcode        string us
d-i     console-setup/variantcode       string
d-i     netcfg/get_nameservers  string
d-i     netcfg/get_ipaddress    string
d-i     netcfg/get_netmask      string 255.255.255.0
d-i     netcfg/get_gateway      string
d-i     netcfg/confirm_static   boolean true
d-i     clock-setup/utc boolean true
d-i     partman-auto/method string regular
d-i     partman-lvm/device_remove_lvm boolean true
d-i     partman-lvm/confirm boolean true
d-i     partman/confirm_write_new_label boolean true
d-i     partman/choose_partition        select Finish partitioning and write changes to disk
d-i     partman/confirm boolean true
d-i     partman/confirm_nooverwrite boolean true
d-i     partman/default_filesystem string ext3
d-i     clock-setup/utc boolean true
d-i     clock-setup/ntp boolean true
d-i     clock-setup/ntp-server  string ntp.ubuntu.com
d-i     base-installer/kernel/image     string linux-server
d-i     passwd/root-login       boolean false
d-i     passwd/make-user        boolean true
d-i     passwd/user-fullname    string ubuntu
d-i     passwd/username string ubuntu
d-i     passwd/user-password-crypted    password $6$.1eHH0iY$ArGzKX2YeQ3G6U.mlOO3A.NaL22Ewgz8Fi4qqz.Ns7EMKjEJRIW2Pm/TikDptZpuu7I92frytmk5YeL.9fRY4.
d-i     passwd/user-uid string
d-i     user-setup/allow-password-weak  boolean false
d-i     user-setup/encrypt-home boolean false
d-i     passwd/user-default-groups      string adm cdrom dialout lpadmin plugdev sambashare
d-i     apt-setup/services-select       multiselect security
d-i     apt-setup/security_host string security.ubuntu.com
d-i     apt-setup/security_path string /ubuntu
d-i     debian-installer/allow_unauthenticated  string false
d-i     pkgsel/upgrade  select safe-upgrade
d-i     pkgsel/language-packs   multiselect
d-i     pkgsel/update-policy    select none
d-i     pkgsel/updatedb boolean true
d-i     grub-installer/skip     boolean false
d-i     lilo-installer/skip     boolean false
d-i     grub-installer/only_debian      boolean true
d-i     grub-installer/with_other_os    boolean true
d-i     finish-install/keep-consoles    boolean false
d-i     finish-install/reboot_in_progress       note
d-i     cdrom-detect/eject      boolean true
d-i     debian-installer/exit/halt      boolean false
d-i     debian-installer/exit/poweroff  boolean false
d-i     pkgsel/include string openssh-server
After PXEBoot installed, you can log in the server with user "ubuntu" and password "ubuntu"

2012年1月12日 星期四

Python write syslog with logging

Python program:
import logging
import logging.handlers

logger = logger.getLogger("NAME")

log_handler = logging.handlers.SysLogHandler(address = "/dev/log")
log_handler.setLevel(logging.DEBUG)
log_handler.setFormatter(logging.Formatter("%(name)s:%(process)d - %(levelname)s - %(pathname)s:%(lineno)d - %(message)s"))

logger.addHandler(log_handler)

logger.debug("This is debug message")
logger.info("This is info message")
logger.warn("This is warning message")
logger.error("This is error message")
logger.critical("This is critical message")

Python write syslog with syslog

Python program:
import syslog

syslog.open(logoption = syslog.LOG_PID)
syslog.syslog("Hello world!")

syslog-ng config

Common section:
@version: 3.2

options {
    long_hostnames(off);
    flush_lines(0);
    use_dns(no);
    use_fqdn(no);
    owner("root");
    group("adm");
    perm(0640);
    stats_freq(0);
    bad_hostname("^gconfd$");
};

Write system log and kernel message to file:
# Record syslog to file
source recorder {
    file("/proc/kmsg");
    unix-dgram("/dev/log");
}
destination recorder {
    file("/var/log/syslog"
        owner(syslog)
        group(adm)
        perm(0644)
        dir_perm(0755)
        create_dirs(yes));
}
log {
    source(recorder);
    destination(recorder);
};

Send local log to remote:
# Send local syslog
source sender {
    file("/var/log/syslog");
};
destination sender {
    tcp(log_1.server port(514));
    tcp(log_2.server port(514));
};
log {
    source(sender);
    destination(sender);
};

Receive log from remote:
# Receive remote syslog
source receiver {
    udp(ip(0.0.0.0) port(514));
    tcp(ip(0.0.0.0) port(514));
};
destination receiver {
    file("/var/log/remote/$YEAR.$MONTH.$DAY/all"
        owner(root)
        group(adm)
        perm(0644)
        dir_perm(0755)
        create_dirs(yes));
    file("/var/log/remote/$YEAR.$MONTH.$DAY/$HOST"
        owner(root)
        group(adm)
        perm(0644)
        dir_perm(0755)
        create_dirs(yes));
};
log {
    source(receiver);
    destination(receiver);
};

Be sure just use "/dev/log" at source section onece.

Reference: http://www.balabit.com/sites/default/files/documents/syslog-ng-ose-3.2-guides/syslog-ng-ose-v3.2-guide-admin-en.html/bk01-toc.html

Python write syslog from config file with logging

Config file:
[loggers]
keys=root,SYS

[logger_root]
level=NOTSET
handlers=screen

[logger_SYS]
handlers=local,remote
qualname=compiler.parser
propagate=1

# Ref: http://docs.python.org/library/logging.html#formatter-objects
[formatters]
keys=simple,complex

[formatter_simple]
format=%(name)s:%(process)d - %(levelname)s - %(message)s

[formatter_complex]
format=%(name)s:%(process)d - %(levelname)s - %(pathname)s:%(lineno)d - %(message)s

# Define handler setting
# Ref: http://docs.python.org/library/logging.handlers.html
[handlers]
keys=screen,local,remote

[handler_screen]
class=StreamHandler
formatter=complex
args=(sys.stdout,)

[handler_local]
class=handlers.SysLogHandler
formatter=complex
args=("/dev/log",)

[handler_remote]
class=handlers.SysLogHandler
formatter=complex
#args=((,),)
args=(("172.16.1.1", 514),)


Python program:
import logging
import logging.config

logging.config.fileConfig("/path/to/config")
logger = logging.getLogger("UI")
logger.debug("This is debug message")
logger.info("This is info message")
logger.warn("This is warning message")
logger.error("This is error message")
logger.critical("This is critical message")

Python generate file with Template by Jinja2

Required: Jinja
Template file: syslog-ng.conf.tpl
context:
@version: 3.2

options {
    long_hostnames(off);
    flush_lines(0);
    use_dns(no);
    use_fqdn(no);
    owner("root");
    group("adm");
    perm(0640);
    stats_freq(0);
    bad_hostname("^gconfd$");
};

# Record syslog to file
source recorder {
    file("/proc/kmsg");
    unix-dgram("/dev/log");
}
destination recorder {
    file("/var/log/syslog"
        owner(syslog)
        group(adm)
        perm(0644)
        dir_perm(0755)
        create_dirs(yes));
}
log {
    source(recorder);
    destination(recorder);
};

# Send local syslog
source sender {
    file("/var/log/syslog");
};
destination sender {
{% for log_server in log_servers %}    tcp({{ log_server }} port(514));
{% endfor %}
#    tcp(172.16.116.240 port(514));
};
log {
    source(sender);
    destination(sender);
};

# Receive remote syslog
source receiver {
    udp(ip(0.0.0.0) port(514));
    tcp(ip(0.0.0.0) port(514));
};
destination receiver {
    file("/var/log/wistor/$YEAR.$MONTH.$DAY/all"
        owner(root)
        group(adm)
        perm(0644)
        dir_perm(0755)
        create_dirs(yes));
    file("/var/log/wistor/$YEAR.$MONTH.$DAY/$HOST"
        owner(root)
        group(adm)
        perm(0644)
        dir_perm(0755)
        create_dirs(yes));
};
log {
    source(receiver);
    destination(receiver);
};


Python program:
from jinja2 import Template

print Template(file("syslog-ng.conf.tpl").read()).render(log_server = [ "172.16.116.%d" % ip for ip in range(1, 11) ])

Reference: http://jinja.pocoo.org/docs/