Difference between revisions of "Ubuntu home server setup steps"

From Nick Jenkins
Jump to: navigation, search
(Setting up UPS monitoring)
m (Enable collection monitoring, including web interface: no longer need to unzip, this is already done by default now; Plus other tweaks needed.)
 
(One intermediate revision by one user not shown)
Line 7: Line 7:
 
Follow these [http://blog.mansonthomas.com/2008/10/setting-up-ups-link-with-ubuntu-server.html blog instructions for setting up NUT] (NUT = network UPS tool).
 
Follow these [http://blog.mansonthomas.com/2008/10/setting-up-ups-link-with-ubuntu-server.html blog instructions for setting up NUT] (NUT = network UPS tool).
  
 +
<!-- Once the nut package is installed, I edited /etc/nut/nut.conf to set:
 +
 +
    MODE=standalone
 +
-->
 
I was setting this machine up to monitor multiple UPSes, so the only differences were that my <code>/etc/nut/ups.conf</code> file looked like this:
 
I was setting this machine up to monitor multiple UPSes, so the only differences were that my <code>/etc/nut/ups.conf</code> file looked like this:
 
<pre>
 
<pre>
Line 34: Line 38:
  
 
... other than that it was all as per the blog post.
 
... other than that it was all as per the blog post.
 +
 +
<!--
 +
Then all that was left to do was to restart nut:
 +
 +
    $ /etc/init.d/nut restart
 +
 +
 +
and check syslog for any errors:
 +
 +
    $ tail /var/log/syslog
 +
-->
  
 
== Enable collection monitoring, including web interface ==
 
== Enable collection monitoring, including web interface ==
Line 49: Line 64:
 
  sudo mkdir bling-collectd
 
  sudo mkdir bling-collectd
 
  cd bling-collectd
 
  cd bling-collectd
  cp /usr/share/doc/collectd/examples/collection.cgi.gz .
+
  cp /usr/share/doc/collectd/examples/collection.cgi .
sudo gunzip collection.cgi.gz
+
 
  sudo chown www-data.www-data collection.cgi .
 
  sudo chown www-data.www-data collection.cgi .
  
For the script to work, your apache config will need the AddHandler line for .cgi files, and also "Options +ExecCGI" for the relevant directory. You should get some basic graphs at the appropriate URL, e.g.: http://bling-collectd/collection.cgi
+
For the script to work, your apache config will need the AddHandler line for .cgi files, and also "Options +ExecCGI" for the relevant directory. You should get some basic graphs at the appropriate URL, e.g.: http://ludo-vm-collectd/collection.cgi
  
 
If you want to also monitor hddtemp using the hddtemp daemon, then do this. Personally, I find the some of the "Sensors" output to be far more accurate than the hddtemp output for monitoring system temp:
 
If you want to also monitor hddtemp using the hddtemp daemon, then do this. Personally, I find the some of the "Sensors" output to be far more accurate than the hddtemp output for monitoring system temp:
Line 71: Line 85:
 
<pre>
 
<pre>
 
#!/bin/bash
 
#!/bin/bash
HOST=localhost
+
#HOST=localhost
 +
HOST=`hostname -f`
 
INTERVAL=300
 
INTERVAL=300
 
while true
 
while true
Line 93: Line 108:
 
</pre>
 
</pre>
  
.... then do <code>sudo visudo</code> and add these two lines to the end:
+
Make it executable:
 +
chmod +x /etc/collectd/exec.sh
 +
 
 +
.... then (only if we're doing SMART hard disk monitoring), we do <code>sudo visudo</code> (may have to first do <code>aptitude install sudo</code> if running Debian stable) and add these two lines to the end:
 
  Cmnd_Alias      SMARTCTL = /usr/sbin/smartctl -d ata -A /dev/sda
 
  Cmnd_Alias      SMARTCTL = /usr/sbin/smartctl -d ata -A /dev/sda
 
  nobody  ALL = (root) NOPASSWD: SMARTCTL
 
  nobody  ALL = (root) NOPASSWD: SMARTCTL
  
... then <code>sudo nano /etc/colllectd/collectd.conf</code> and load the exec module, and make the exec plugin section look like this:
+
... then <code>sudo nano /etc/collectd/collectd.conf</code> and load the exec module (by uncommenting the <code>#LoadPlugin exec</code> line), and make the commented-out exec plugin section look like this:
 
  <Plugin exec>
 
  <Plugin exec>
 
         Exec "nobody" "/etc/collectd/exec.sh"
 
         Exec "nobody" "/etc/collectd/exec.sh"
 
  </Plugin>
 
  </Plugin>
 +
... and whilst editing this file, can also comment out modules that we typically don't care about, namely:
 +
#LoadPlugin entropy
 +
#LoadPlugin irq
 +
#LoadPlugin users
 +
 +
Then restart collectd:
 +
/etc/init.d/collectd restart
  
 
.... the above will give you a graph of days uptime, and number of uncorrectable SMART errors. Note that the y axis will be wrong (will be mislabeled as "charge" in "Ah", when for the uptime graph it will be number of days, and for the smartmon graph it will be number of uncorrectable errors), but the information is otherwise correct.
 
.... the above will give you a graph of days uptime, and number of uncorrectable SMART errors. Note that the y axis will be wrong (will be mislabeled as "charge" in "Ah", when for the uptime graph it will be number of days, and for the smartmon graph it will be number of uncorrectable errors), but the information is otherwise correct.
  
 +
If you're running Debian stable, and you get Perl code as the output in the web page, then there are a few extra steps needed to resolve this:
 +
aptitude install libapache2-mod-perl2
 +
nano /etc/apache2/mods-available/mime.conf
 +
Change this line:
 +
#AddHandler cgi-script .cgi
 +
to this:
 +
AddHandler cgi-script .cgi .pl
 +
Then do:
 +
nano /etc/apache2/sites-available/default
 +
and add: <code>ExecCGI</code> to the relevant <code>Options</code> line, so that the CGI can run.
 +
Then lastly, restart the web server:
 +
/etc/init.d/apache2 restart
 +
 +
If you ever want to reset the stats, they are stored under <code>/var/lib/collectd/rrd/`hostname -f`</code> - so you can stop collectd and delete unwanted stats, then restart collectd.
 
<!--  
 
<!--  
 
2009-Apr-7: what's not working:
 
2009-Apr-7: what's not working:
Line 109: Line 148:
  
 
-->
 
-->
 +
 
== Set up Rtorrent to use your off-peak data allowance ==
 
== Set up Rtorrent to use your off-peak data allowance ==
  

Latest revision as of 06:40, 18 August 2010

If you have a linux box at home that's running 24x7, then there are a few useful things you can do with it, including the following:

Setting up UPS monitoring[edit]

Note: Only needed if you have a UPS that you want to montior, and a data cable connecting it to your machine.

Follow these blog instructions for setting up NUT (NUT = network UPS tool).

I was setting this machine up to monitor multiple UPSes, so the only differences were that my /etc/nut/ups.conf file looked like this:

[UPSONIC-DS800]
driver = megatec_usb
port = auto
desc = "UPSONIC DS-800 UPS"

[APC-BackUPS-ES-500]
driver = usbhid-ups
port = auto
desc = "APC BackUPS ES 500"

[Eaton-Powerware-5110]
driver = megatec_usb
port = auto
desc = "Eaton Powerware 5110, 700VA"

... and my /etc/nut/upsmon.conf file had the following monitor lines, corresponding to the UPSes defined above:

MONITOR APC-BackUPS-ES-500@localhost 1 nickj test master
MONITOR UPSONIC-DS800@localhost 1 nickj test master
MONITOR Eaton-Powerware-5110@localhost 1 nickj test master

... other than that it was all as per the blog post.


Enable collection monitoring, including web interface[edit]

Commands as follows to install collectd and set up a web interface:

sudo aptitude install collectd librrds-perl lm-sensors
sudo nano /etc/collectd/collectd.conf 
# Uncomment sensors, and nut (if you have a UPS and configured NUT in the above step).
# Note: Nut in Ubuntu prior to 9.04 has a bug so that NUT data won't be visible on AMD64 using collectd
sudo /etc/init.d/collectd restart
# The location of your webroot may differ - the below line is appropriate for
# my configuration, but modify as needed:
cd /var/www/hosts/
sudo mkdir bling-collectd
cd bling-collectd
cp /usr/share/doc/collectd/examples/collection.cgi .
sudo chown www-data.www-data collection.cgi .

For the script to work, your apache config will need the AddHandler line for .cgi files, and also "Options +ExecCGI" for the relevant directory. You should get some basic graphs at the appropriate URL, e.g.: http://ludo-vm-collectd/collection.cgi

If you want to also monitor hddtemp using the hddtemp daemon, then do this. Personally, I find the some of the "Sensors" output to be far more accurate than the hddtemp output for monitoring system temp:

sudo aptitude install hddtemp
# yes to install hddtemp as a daemon
sudo dpkg-reconfigure hddtemp
* yes suid
* 30 seconds between checks
* Should the hddtemp daemon be started at boot?     yes
* Interface to listen on:    127.0.0.1
# then enable the hddtemp module by editing this file:
 sudo nano /etc/collectd/collectd.conf

If you want to also monitor uptime, and S.M.A.R.T. errors using smartmontools, then do this:

sudo nano /etc/collectd/exec.sh and paste in the following:

#!/bin/bash
#HOST=localhost
HOST=`hostname -f`
INTERVAL=300
while true
do
	TEMP=`awk '{ print $1/(60*60*24); }' /proc/uptime`
        if [ $? -ne 0 ]
        then
                TEMP="U"
        fi
        echo "PUTVAL $HOST/exec-uptime/charge-uptime interval=$INTERVAL N:$TEMP"

	TEMP=$((sudo smartctl -d ata -A /dev/sda | grep Offline_Uncorrectable | awk '{ print $10; }') 2>/dev/null);
	if [ $? -ne 0 ]
	then
		TEMP="U"
	fi
	echo "PUTVAL $HOST/exec-smartmon/charge-offline_uncorrectable interval=$INTERVAL N:$TEMP"

	sleep $INTERVAL
done

Make it executable:

chmod +x /etc/collectd/exec.sh 

.... then (only if we're doing SMART hard disk monitoring), we do sudo visudo (may have to first do aptitude install sudo if running Debian stable) and add these two lines to the end:

Cmnd_Alias      SMARTCTL = /usr/sbin/smartctl -d ata -A /dev/sda
nobody   ALL = (root) NOPASSWD: SMARTCTL

... then sudo nano /etc/collectd/collectd.conf and load the exec module (by uncommenting the #LoadPlugin exec line), and make the commented-out exec plugin section look like this:

<Plugin exec>
       Exec "nobody" "/etc/collectd/exec.sh"
</Plugin>

... and whilst editing this file, can also comment out modules that we typically don't care about, namely:

#LoadPlugin entropy
#LoadPlugin irq
#LoadPlugin users

Then restart collectd:

/etc/init.d/collectd restart

.... the above will give you a graph of days uptime, and number of uncorrectable SMART errors. Note that the y axis will be wrong (will be mislabeled as "charge" in "Ah", when for the uptime graph it will be number of days, and for the smartmon graph it will be number of uncorrectable errors), but the information is otherwise correct.

If you're running Debian stable, and you get Perl code as the output in the web page, then there are a few extra steps needed to resolve this:

aptitude install libapache2-mod-perl2
nano /etc/apache2/mods-available/mime.conf

Change this line:

#AddHandler cgi-script .cgi

to this:

AddHandler cgi-script .cgi .pl

Then do:

nano /etc/apache2/sites-available/default

and add: ExecCGI to the relevant Options line, so that the CGI can run. Then lastly, restart the web server:

/etc/init.d/apache2 restart

If you ever want to reset the stats, they are stored under /var/lib/collectd/rrd/`hostname -f` - so you can stop collectd and delete unwanted stats, then restart collectd.

Set up Rtorrent to use your off-peak data allowance[edit]

Some ISPs will give you an allowance of peak data, and allowance of off-peak data. Peak is sometimes quite long (e.g. 18 hours), and off-peak can be quite short (e.g. 6 hours) and is usually during the middle of the night. Usually most of that off-peak bandwidth goes to waste. One way to use it efficiently is to use rtorrent to automate downloading during your off-peak times. To do this:

aptitude install rtorrent screen
cd
mkdir Downloads
cd Downloads/
mkdir rtorrent-session rtorrent-inprogress rtorrent-watch rtorrent-completed
cp /usr/share/doc/rtorrent/examples/rtorrent.rc ~/.rtorrent.rc
nano ~/.rtorrent.rc

... and follow this great guide for modifying the file for your needs. I also make the following changes:

# Global upload and download rate in KiB. "0" for unlimited.
download_rate = 0
upload_rate = 97
# Upload rate should be explicitly set to 80% of your max upload rate. Higher than
# this, and you will get slower downloads because the link will be too congested.

# Move completed torrents downloads, and delete the associated torrent file:
on_finished = rm_torrent,"execute=rm,$d.get_tied_to_file="
on_finished = move_complete,"execute=mv,-u,$d.get_base_path=,/home/username/Downloads/rtorrent-completed/ \
;d.set_directory=/home/username/Downloads/rtorrent-completed/"

# Controls if the session torrent is saved when a torrent finishes. We don't want it.
session_on_completion = false

To set it up to run regularly as a daemon:

wget http://libtorrent.rakshasa.no/raw-attachment/wiki/RTorrentCommonTasks/rtorrentInit.sh
nano rtorrentInit.sh
# replace the run as username with your username used in the section above 
sudo mv rtorrentInit.sh /etc/init.d/rtorrent
sudo chown root.root /etc/init.d/rtorrent
sudo chmod +x /etc/init.d/rtorrent
sudo nano /etc/crontab

... and add two lines like so:

# Start bitorrent downloading at 1 AM every night, and stop at 6:59 AM every morning
00 1    * * *   root    /etc/init.d/rtorrent start
59 6    * * *   root    /etc/init.d/rtorrent stop

Rtorrent does not currently have native ipfiltering, so you have to set it up using a firewall. Moblock is very good for this, and there are very good MoBlock setup instructions available. Here's a summary for Ubuntu 8.10:

gpg --keyserver wwwkeys.eu.pgp.net --recv 9072870B
gpg --export --armor 9072870B | sudo apt-key add -
sudo nano /etc/apt/sources.lst

... and add these lines:

# MoBlock packages:
deb http://moblock-deb.sourceforge.net/debian intrepid main
deb-src http://moblock-deb.sourceforge.net/debian intrepid main

... then do:

sudo aptitude update
sudo aptitude install moblock blockcontrol
sudo blockcontrol restart
sudo blockcontrol status
sudo blockcontrol test

If you need to reconfigure moblock, then use: sudo dpkg-reconfigure blockcontrol