| 1 | #!/usr/bin/php |
| 2 | <?php |
| 3 | $refresh = 1; |
| 4 | $battery = "/proc/acpi/battery/BAT1"; |
| 5 | $critical = "echo Shutting down... - Low Battery"; |
| 6 | $warning = "echo Warning!!! - Low Battery"; |
| 7 | |
| 8 | ///CHECK_BATT |
| 9 | function check_battery($bat = "/proc/acpi/battery/BAT1", $critical = "echo Shutting down... - Low Battery", $warning = "echo Warning!!! - Low Battery") { |
| 10 | $info = fopen($bat.'/info', 'r'); |
| 11 | while(!feof($info)) { |
| 12 | $line = fgets($info); |
| 13 | if(eregi('design capacity warning', $line)) { |
| 14 | $line = explode(' ', $line); |
| 15 | //print_r($line); |
| 16 | $warncap = $line[3]; |
| 17 | } |
| 18 | if(eregi('design capacity low', $line)) { |
| 19 | $line = explode(' ', $line); |
| 20 | //print_r($line); |
| 21 | $lowcap = $line[7]; |
| 22 | } |
| 23 | } |
| 24 | fclose($info); |
| 25 | |
| 26 | $dischg = 'Chg'; |
| 27 | $state = fopen($bat.'/state', 'r'); |
| 28 | while(!feof($state)) { |
| 29 | $line = fgets($state); |
| 30 | if(eregi('discharging', $line)) { |
| 31 | $dischg = 'Dischg'; |
| 32 | } |
| 33 | if(eregi('remaining capacity', $line)) { |
| 34 | $line = explode(' ', $line); |
| 35 | //print_r($line); |
| 36 | $remaincap = $line[7]; |
| 37 | } |
| 38 | } |
| 39 | fclose($state); |
| 40 | |
| 41 | echo("Remaining capacity $remaincap mAh (Critical: $lowcap mAh; Warning: $warncap mAh) $dischg \r"); |
| 42 | if($dischg == 'Dischg') { |
| 43 | if($remaincap < $lowcap) { echo("\n"); system($critical); } |
| 44 | if($remaincap < $warncap) { echo("\n"); system($warning); } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | ///LOOP |
| 49 | echo("Watching battery $battery\n"); |
| 50 | while(1) { |
| 51 | check_battery($battery, $critical, $warning); |
| 52 | sleep($refresh); |
| 53 | } |