Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
110 lines (102 sloc) 3.27 KB
AWSTemplateFormatVersion: "2010-09-09"
Description: Freifunk Monitor for your Router
Parameters:
AccessPointName:
Type: String
NotificationEmail:
Type: String
CheckRateMinutes:
Type: Number
Default: 5
Resources:
CheckProgram:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: !Sub |
import json
import urllib2
import boto3
cw = boto3.client('cloudwatch')
def handler(event, context):
api_result = json.loads(urllib2.urlopen("https://api.freifunk.net/data/freifunk-karte-data.json").read())
found_access_points = [access_point for access_point in api_result["allTheRouters"] if access_point["name"] == "${AccessPointName}"]
if len(found_access_points) != 1:
raise "Did not find an Access Point with the given name"
found_access_point = found_access_points[0]
# todo: implement other freifunk communities
local_api_result = json.loads(urllib2.urlopen("https://map.hamburg.freifunk.net/nodes.json").read())
access_point_is_online = local_api_result["nodes"][found_access_point["id"]]["flags"]["online"]
cw.put_metric_data(
Namespace="FreifunkAccessPoint",
MetricData=[{
"MetricName": "online",
"Dimensions": [{
"Name": "AccessPointName",
"Value": "${AccessPointName}"
}],
"Value": 1 if access_point_is_online else 0
}]
)
Handler: index.handler
Role:
Fn::GetAtt:
- CheckProgramRole
- Arn
Runtime: python2.7
Timeout: 300
MemorySize: 256
CheckProgramRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: WriteCustomMetric
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- cloudwatch:PutMetricData
Resource: "*"
CheckProgramTrigger:
Type: "AWS::Events::Rule"
Properties:
ScheduleExpression: !Sub rate(${CheckRateMinutes} minutes)
Targets:
- Arn:
!GetAtt [CheckProgram, Arn]
Id: InvokeLambda
AccessPointOfflineAlertTopic:
Type: "AWS::SNS::Topic"
Properties:
Subscription:
- Endpoint: !Ref NotificationEmail
Protocol: email
AccessPointOfflineAlert:
Type: "AWS::CloudWatch::Alarm"
Properties:
AlarmActions:
- !Ref AccessPointOfflineAlertTopic
OKActions:
- !Ref AccessPointOfflineAlertTopic
InsufficientDataActions:
- !Ref AccessPointOfflineAlertTopic
ComparisonOperator: LessThanThreshold
Dimensions:
- Name: AccessPointName
Value: !Ref AccessPointName
EvaluationPeriods: 1
MetricName: online
Namespace: FreifunkAccessPoint
Period: 60
Statistic: Maximum
Threshold: 1