| 1 | #! /usr/bin/awk -f |
| 2 | |
| 3 | BEGIN { |
| 4 | OFS = msg_fs ? msg_fs : "|" |
| 5 | Kfs = key_fs ? key_fs : ":" |
| 6 | } |
| 7 | |
| 8 | # When parsing 'upower --dump' |
| 9 | /^Device:[ \t]+/ { |
| 10 | device["path"] = $2 |
| 11 | next |
| 12 | } |
| 13 | |
| 14 | # When parsing 'upower --monitor-detail' |
| 15 | /^\[[0-9]+:[0-9]+:[0-9]+\.[0-9]+\][ \t]+device changed:[ \t]+/ { |
| 16 | device["path"] = $4 |
| 17 | next |
| 18 | } |
| 19 | |
| 20 | # BEGIN battery |
| 21 | / battery/ && device["path"] { |
| 22 | device["is_battery"] = 1 |
| 23 | next |
| 24 | } |
| 25 | |
| 26 | / state:/ && device["is_battery"] { |
| 27 | device["battery_state"] = $2 |
| 28 | next |
| 29 | } |
| 30 | |
| 31 | / percentage:/ && device["is_battery"] { |
| 32 | device["battery_percentage"] = $2 |
| 33 | sub("%$", "", device["battery_percentage"]) |
| 34 | next |
| 35 | } |
| 36 | |
| 37 | /^$/ && device["is_battery"] { |
| 38 | print("battery_state" , device["battery_state"]) |
| 39 | print("battery_percentage", device["battery_percentage"]) |
| 40 | } |
| 41 | # END battery |
| 42 | |
| 43 | # BEGIN line-power |
| 44 | / line-power/ && device["path"] { |
| 45 | device["is_line_power"] = 1 |
| 46 | next |
| 47 | } |
| 48 | |
| 49 | / online:/ && device["is_line_power"] { |
| 50 | device["line_power_online"] = $2 |
| 51 | next |
| 52 | } |
| 53 | |
| 54 | /^$/ && device["is_line_power"] { |
| 55 | print("line_power", device["line_power_online"]) |
| 56 | } |
| 57 | # END line-power |
| 58 | |
| 59 | /^$/ { |
| 60 | delete device |
| 61 | next |
| 62 | } |