Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM alpine:3.9

RUN apk --no-cache update \
&& apk add --no-cache \
rsyslog \
rsyslog-elasticsearch \
rsyslog-imrelp \
rsyslog-mmjsonparse \
rsyslog-mmutf8fix \
rsyslog-omrelp \
python3 \
py3-requests

RUN \
echo "" >> /etc/rsyslog.conf && \
echo "module(load=\"imudp\")" >> /etc/rsyslog.conf && \
echo "input(type=\"imudp\" port=\"514\" ruleset=\"apple_airport\")" >> /etc/rsyslog.conf && \
echo "module(load=\"omprog\")" >> /etc/rsyslog.conf && \
echo "ruleset(name=\"apple_airport\"){" >> /etc/rsyslog.conf && \
echo "action(type=\"omprog\" binary=\"/opt/apple_airport.py\")" >> /etc/rsyslog.conf && \
echo "}" >> /etc/rsyslog.conf

COPY apple_airport.py /opt/apple_airport.py

RUN chmod +x /opt/apple_airport.py

CMD ["/usr/sbin/rsyslogd","-n", "-d"]

EXPOSE 514/udp
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,8 @@ device_tracker:
track_new_devices: false
hide_if_away: true
```

# Docker
To build docker image run `docker build -f Dockerfile -t ha-airport-rsyslog .`
To run rsyslog in docker run smth like this `docker run --rm -it -p 514:514/udp -e HA_HOST=my_home_assistant_host -e HA_PORT=my_home_assistant_port -e HA_TOKEN=My_Long-Lived_Access_Token --name rsyslog rsyslog`
To create long-lived token use home assistant cotrol panel or read https://developers.home-assistant.io/docs/auth_api/#long-lived-access-token.
19 changes: 14 additions & 5 deletions apple_airport.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
#!/usr/bin/python3
from requests import post
headers = {'x-ha-access': 'http_password', 'content-type': 'application/json'}

import os

host = os.getenv('HA_HOST', '127.0.0.1')
port = os.getenv('HA_PORT', '8123')
token = os.getenv('HA_TOKEN', 'NO TOKEN')

endpoint = 'http://' + host + ':' + port
headers = {'Authorization': 'Bearer ' + token, 'content-type': 'application/json'}

while True:
try:
str = input()
except EOFError:
break
if 'pppoe: Disconnected.' in str or 'ether: (WAN) link state is Down' in str:
postData = '{"state":"off","attributes": {"wan_ip":""}}'
post('http://127.0.0.1:8123/api/states/binary_sensor.Internet',data=postData,headers=headers)
post(endpoint + '/api/states/binary_sensor.Internet',data=postData,headers=headers)
elif 'pppoe: Connection established' in str:
wan_ip = str[str.find('established')+12:str.find('->')-1]
postData = '{"state":"on","attributes": {"wan_ip":"'+wan_ip+'"}}'
post('http://127.0.0.1:8123/api/states/binary_sensor.Internet',data=postData,headers=headers)
post(endpoint + '/api/states/binary_sensor.Internet',data=postData,headers=headers)
elif 'Disassociated with station' in str:
mac = str[-17:len(str)]
postData = '{"mac":"'+mac+'","source_type":"router","consider_home":"3"}'
post('http://127.0.0.1:8123/api/services/device_tracker/see',data=postData,headers=headers)
post(endpoint + '/api/services/device_tracker/see',data=postData,headers=headers)
elif 'Installed unicast CCMP key for supplicant' in str:
mac = str[-17:len(str)]
postData = '{"mac":"'+mac+'","source_type":"router","consider_home":"99:00:00"}'
post('http://127.0.0.1:8123/api/services/device_tracker/see',data=postData,headers=headers)
post(endpoint + '/api/services/device_tracker/see',data=postData,headers=headers)