Home server : auto-shutdown if no other computers are running

I wanted to shutdown my linux home media server if there is no running computer on my network. So I wrote this little programs which reads all known ips from DHCP configuration and lease files and send a ping to them. If the ping respond, one PC of my LAN is up…

To re-start the computer in the morning, I use the BIOS RTC alarm (the thing you have by pressing F1 or ESC on reboot). You could also add a script/a program on each of your computers to send the magic packet to your home server to wake it by lan (see “wake on lan” on google).

This script can take any command. But if you want to do shutdown like proposed in the title, you can use :

[code]sudo ./autoshut “poweroff” 10.0.0.1[/code]

Where 10.0.0.1 is your local IP adress. To compile the program, simply use (after saving the code as “autoshut.c”) : [code]gcc -o autoshut autoshut.c[/code]

In my case, I wanted to launch the command only after midnight, so I used cron. Cron will launch that command every 5 minutes from midnight to eight o’clock. So if I stay up late, my server won’t shutdown if my own computer is not down too. That’s the whole purpose.

The line in my crontab :

[code]0,10,20,30,40,50 1,2,3,4,5,6,7 * * * root /home/tom/autoshut/autoshut “poweroff” 10.0.0.1[/code]

Why do all that ? Energy consumption…

[code]#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
#include <string.h>

#define LEASE_FILE “/var/lib/dhcp/dhcpd.leases”
#define DHCP_CONFIG_FILE “/etc/dhcp/dhcpd.conf”

int in_array(char** ar, char* str) {
int i = 0;
while (ar[i] != NULL) {
if (strcmp(ar[i],str) == 0) return 1;
i++;
}
return 0;
}

char* extractIP (char* filename, char** list, int* listNum) {
FILE *pfile;

pfile = fopen(filename, “rb”);

if(pfile == NULL){
printf(“Sorry, can’t open %s\n”, filename);
return ‘\0’;
}
regex_t reg;
int err = regcomp (&reg, “(10\\.[0-1]\\.0\\.([1-9]|[0-9]{2,3}))”, REG_EXTENDED);
if (err != 0)
{
printf(“ERREUR\n”);
return ‘\0’;
}
char ligne[255];

while(!feof(pfile)) {
fgets(ligne, 254 ,pfile);
int match;
size_t nmatch = 0;
regmatch_t *pmatch = NULL;

nmatch = reg.re_nsub;
pmatch = malloc (sizeof (*pmatch) * nmatch);
match = regexec (&reg, ligne, nmatch, pmatch, 0);
if (match == 0)
{
char *ip = NULL;
int start = pmatch->rm_so;
int end = pmatch->rm_eo;
size_t size = end – start;

char* str = malloc(sizeof(char) * 15);
strncpy (str, &ligne[start], size);
str[size] = ‘\0’;

if (!in_array(list, str)) {
list[*listNum] = str;
printf(“%s\n”, str);
(*listNum)++;
}

}

}

fclose(pfile);
regfree(&reg);
}

int ping(char* address) {

char cmd[30] = “ping -W 1 -q -c 1 “;
strcat(cmd,address);
int ret=system(cmd);
printf(“\nResultat : %d\n\n”,ret);
return !ret;
}
/*
TODO : maybe an option?
int checkCableStatus(const char* interface) {
/sys/class/net/eth0/carrier
}*/

int main(int argc, char** argv) {

if (argc <= 2) {
printf(“Usage : %s Command Local-IP [Local-IP-2]\n\tCommand : a command to execute if ping does not work\n\tLocal-IP : Ip to ignore\n\tLocal-IP-2 : Optional second ip to ignore”,argv[0]);
return -1;
}
int listNum = 0;
char** list = malloc(sizeof(char*) * 255);
extractIP(DHCP_CONFIG_FILE, list, &listNum);
extractIP(LEASE_FILE, list, &listNum);
int i = 0;
while (list[i] != NULL) {
if (strcmp(list[i],argv[2])!=0 && (argc==3 ||strcmp(list[i],argv[3])!=0)) {
if (ping (list[i])) {
printf(“%s responded. Command aborted !\n”,list[i]);
return EXIT_SUCCESS;
}
}
i++;
}
system(argv[1]);
return EXIT_SUCCESS;
}[/code]

HDParm

HDParm is a tool to manage your drives. The first section show you how to get informations about your drives, the second tells you how to make them sleep when they aren’t used (and so make power economy). If you’ve got a home server like me with the system on a SSD, with only your big files on the hard drives(s), you’ll want to do this.

 

Get disk informations

You’ve got to use “sudo” to gain super user privilege. Your disk are in the /dev/ folder, named sd* where “*” is a letter. To see the list of disk, one can type use “ls /dev/sd[a-z]“:
debserver % ls /dev/sd[a-z]
/dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde

 

To display informations about one disk, you can use “sudo hdparm -i”

 

debserver % sudo hdparm -i /dev/sda
/dev/sda:

Model=WDC WD20EFRX-68EUZN0, FwRev=80.00A80, SerialNo=WD-WCC4M1067803
Config={ HardSect NotMFM HdSw>15uSec SpinMotCtl Fixed DTR>5Mbs FmtGapReq }
RawCHS=16383/16/63, TrkSize=0, SectSize=0, ECCbytes=0
BuffType=unknown, BuffSize=unknown, MaxMultSect=16, MultSect=16
CurCHS=16383/16/63, CurSects=16514064, LBA=yes, LBAsects=3907029168
IORDY=on/off, tPIO={min:120,w/IORDY:120}, tDMA={min:120,rec:120}
PIO modes: pio0 pio3 pio4
DMA modes: mdma0 mdma1 mdma2
UDMA modes: udma0 udma1 udma2 udma3 udma4 udma5 *udma6
AdvancedPM=no WriteCache=enabled
Drive conforms to: Unspecified: ATA/ATAPI-1,2,3,4,5,6,7

* signifies the current active mode

 

Make a drive sleep after some time

From the manual (man hdparm on the command line):

-S Set the standby (spindown) timeout for the drive. This value is used by the drive to determine how long to wait (with no disk activity) before turning off the spindle motor to save power. Under such circumstances, the drive may take as long as 30 seconds to respond to a subsequent disk access, though most drives are much quicker. The encoding of the timeout value is somewhat peculiar. A value of zero means "timeouts are disabled": the device will not automatically enter standby mode. Values from 1 to 240 specify multiples of 5 seconds, yielding timeouts from 5 seconds to 20 minutes. Values from 241 to 251 specify from 1 to 11 units of 30 minutes, yielding timeouts from 30 minutes to 5.5 hours. A value of 252 signifies a timeout of 21 minutes. A value of 253 sets a vendor-defined timeout period between 8 and 12 hours, and the value 254 is reserved. 255 is interpreted as 21 minutes plus 15 seconds. Note that some older drives may have very different interpretations of these values.

 

So “sudo hdparm -I /dev/sdb | grep level” will show the current spindown value, for Example:

debserver % sudo hdparm -I /dev/sdb | grep level
Advanced power management level: 254

Example:

sudo hdparm -S 25 /dev/sdb = spindown after 25*5 seconds.

sudo hdparm -S 245 /dev/sdb = spindown after (245-240)*30 minutes.

 

If you want to check the state to see if it works :

sudo watch -n 1 "hdparm -C /dev/sdb && hdparm -C /dev/sdc && hdparm -C /dev/sdd && hdparm -C /dev/sde"

 

You can use /etc/hdparm.conf to make it permanent:
/dev/sdb {
spindown_time = 60
}