PDF toolkit – split/merge PDFs

I had some schoolwork scanned to a PDF-document, with odd pages in one document and even pages in another. The pages was also in wrong order. I was looking for an easy way to rearrange the pages in a single document. No fancy interface reacquired, a understandable command line would do. PDF toolkit is this.

Install

apt-get install pdftk

Merge

pdftk A=file1.pdf B=file2.pdf cat A1 B2 A2 B1 output output.pdf

This will make an PDF document “output.pdf” with four pages. Page 1 in output.pdf being the first page from file1.pdf, page 2 being the second page from file2.pdf and so on.

LXC container and default route

After I installed a new switch, attached to my router, I got some bizarre network problem with my server. The server that host this website, was not available from the Internet. I could browse the website from LAN, but not from outside the router.

I tried taking away the new switch, changing the port forward, upgrading the router firmware. Nothing helped. By a lucky shot, I tried to reach the Internet with ping from inside the LXC host. “No route to host”.

I imagine I have restarted the server earlier, and I assume that something must have set the default route earlier. But now I was not able to find any of this setting inside the LXC host. “grep -i “default gw” /etc/*” did not find anything.

So, simple fix:
add "route add default gw x.x.x.x" to /etc/rc.local
chmod +x /etc/rc.local

monit – monitor your services

After I got my home server up running, I was looking for a easy way to monitor it services. Earlier I’ve used MRTG, Smokeping, Cacti and similar, but this time I was looking for something really easy and lightweight. The graphs in MRTG and similar is useful to have, but my main focus was to monitor the health of a service and alert if it’s not.

When I found Monit, I understood that monitor software with web front end, graphs and user management(Zabbix, Zenoss core, with more) was an total overkill for my task. Monit is easy to use, repairs the faulty service and have a nice web front end to see the current status.

Install

Since Monit comes with Ubuntu, it’s easy:

apt-get install monit

Edit monit to start. Change startup=0 to startup=1

nano /etc/default/monit

Edit /etc/monit/monitrc. The file is self explainable and there is good documentation on http://mmonit.com/monit/documentation/monit.html. My stripped file looks like this:

set daemon  120           # check services at 2-minute intervals
with start delay 60  # optional: delay the first check by 1 minute
set logfile syslog facility log_daemon
set mailserver localhost,               # primary mailserver
set mail-format { from: monit@mydomain.no } # some mailservers bounce domains not found in DNS
set alert arve@mydomain.no
set httpd port 2812 and
use address localhost  # only accept connection from localhost
allow localhost        # allow localhost to connect to the server and
allow monit:passwd   # allow user monit with password passwd
include /etc/monit/conf.d/* # include conf files

I then created files in /etc/monit/conf.d. Here is my localhost file.

check system localhost
if loadavg(5min) > 4 then alert
if loadavg(15min) > 2 then alert
# filesystems
check filesystem root with path /
if space usage > 80% then alert
check filesystem home with path /home
if space usage > 95% then alert
# cron
check process cron with pidfile /var/run/crond.pid
start program = "/etc/init.d/cron start"
stop  program = "/etc/init.d/cron stop"
if 5 restarts within 5 cycles then timeout
# sshd
check process sshd with pidfile /var/run/sshd.pid
start program = "/etc/init.d/ssh start"
stop program = "/etc/init.d/ssh stop"
if failed port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout
#  samba
check process smbd with pidfile /var/run/samba/smbd.pid
start program = "/etc/init.d/smbd start"
stop  program = "/etc/init.d/smbd stop"
if failed host 192.168.1.2 port 139 type TCP  then restart
if 5 restarts within 5 cycles then timeout
#  nfs
check host server.lan with address 192.168.1.2
start = "/etc/init.d/nfs-kernel-server start"
stop = "/etc/init.d/nfs-kernel-server stop"
if failed port 2049 then restart

Services are automagically restarted and you are alerted by email if pid file does not exist, or there is no running service with that pid. If statements holds extra tests for services, and services without pid file can be watched by checking the service over network.

Also services on the network can be watched. Here is another server I’m watching:

check host seljebu.no with address seljebu.no
if failed port 143 proto imap with timeout 2 seconds then alert
if failed port 465 type tcpssl proto smtp with timeout 2 seconds then alert
if failed url http://seljebu.no/ with timeout 2 seconds then alert

Restart monit.

service monit restart

Look for errors.

tail /var/log/syslog

If you get error message /etc/monit/conf.d/host:12: Error: syntax error ‘=’, change all instances of ” with ".

Thats it!