This repository has been archived by the owner on Oct 13, 2018. It is now read-only.
forked from s0enke/serverless-freifunk-alarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.yaml
116 lines (109 loc) · 3.53 KB
/
app.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
CheckProgramTriggerInvokePermission:
Type: "AWS::Lambda::Permission"
Properties:
FunctionName: !GetAtt CheckProgram.Arn
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"
SourceArn: !GetAtt CheckProgramTrigger.Arn
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