-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzmeventnotification.pl
executable file
·213 lines (178 loc) · 5.5 KB
/
zmeventnotification.pl
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/perl -wT
#
# ==========================================================================
#
# ZoneMinder Event Watch Script, $Date$, $Revision$
#
# A very light weight event notification daemon
# Uses shared memory to detect new events (polls SHM)
# Also opens a websocket connection at a configurable port
# so events can be reported
# Any client can connect to this web socket and handle it further
# for example, send it out via APNS/GCM or any other mechanism
#
# This is a much faster and low overhead method compared to zmfilter
# as there is no DB overhead nor SQL searches for event matches
# ~ PP
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ==========================================================================
use strict;
use bytes;
use Net::WebSocket::Server;
# ==========================================================================
#
# These are the elements you can edit to suit your installation
#
# ==========================================================================
use constant SLEEP_DELAY=>5; # duration in seconds after which we will check for new events
use constant MONITOR_RELOAD_INTERVAL => 300;
use constant EVENT_NOTIFICATION_PORT=>9000; # port for Websockets connection
# ==========================================================================
#
# Don't change anything below here
#
# ==========================================================================
use lib '/usr/local/lib/x86_64-linux-gnu/perl5';
use ZoneMinder;
use POSIX;
use DBI;
use Data::Dumper;
#use CGI::Cookie;
#use PHP::Session;
#my %cookies = fetch CGI::Cookie;
#my $session = PHP::Session->new("PHPSESSID");
#print Dumper($session);
#exit;
$| = 1;
$ENV{PATH} = '/bin:/usr/bin';
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
sub Usage
{
print( "This daemon is not meant to be invoked from command line\n");
exit( -1 );
}
logInit();
logSetSignal();
Info( "Event Notification daemon starting\n" );
my $dbh = zmDbConnect();
my %monitors;
my $monitor_reload_time = 0;
my $wss;
my $evt_str="";
initSocketServer();
Info( "Event Notification daemon exiting\n" );
exit();
sub checkEvents()
{
my $eventFound = 0;
$evt_str="";
if ( (time() - $monitor_reload_time) > MONITOR_RELOAD_INTERVAL )
{
Debug ("Reloading Monitors...\n");
foreach my $monitor (values(%monitors))
{
zmMemInvalidate( $monitor );
}
loadMonitors();
}
foreach my $monitor ( values(%monitors) )
{
my ( $state, $last_event )
= zmMemRead( $monitor,
[ "shared_data:state",
"shared_data:last_event"
]
);
if ($state == STATE_ALARM || $state == STATE_ALERT)
{
if ( !defined($monitor->{LastEvent})
|| ($last_event != $monitor->{LastEvent}))
{
Info( "New event $last_event reported for ".$monitor->{Name}."\n");
$monitor->{LastState} = $state;
$monitor->{LastEvent} = $last_event;
$evt_str = $evt_str.$monitor->{Name}.":".$monitor->{Id}.":".$last_event.",";
$eventFound = 1;
}
}
}
return ($eventFound);
}
sub loadMonitors
{
Debug( "Loading monitors\n" );
$monitor_reload_time = time();
my %new_monitors = ();
my $sql = "SELECT * FROM Monitors
WHERE find_in_set( Function, 'Modect,Mocord,Nodect' )"
;
my $sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute()
or Fatal( "Can't execute: ".$sth->errstr() );
while( my $monitor = $sth->fetchrow_hashref() )
{
next if ( !zmMemVerify( $monitor ) ); # Check shared memory ok
if ( defined($monitors{$monitor->{Id}}->{LastState}) )
{
$monitor->{LastState} = $monitors{$monitor->{Id}}->{LastState};
}
else
{
$monitor->{LastState} = zmGetMonitorState( $monitor );
}
if ( defined($monitors{$monitor->{Id}}->{LastEvent}) )
{
$monitor->{LastEvent} = $monitors{$monitor->{Id}}->{LastEvent};
}
else
{
$monitor->{LastEvent} = zmGetLastEvent( $monitor );
}
$new_monitors{$monitor->{Id}} = $monitor;
}
%monitors = %new_monitors;
}
sub initSocketServer
{
checkEvents();
Info ("Web Socket Event Server listening on port ".EVENT_NOTIFICATION_PORT."\n");
$wss = Net::WebSocket::Server->new(
listen => EVENT_NOTIFICATION_PORT,
tick_period => SLEEP_DELAY,
on_tick => sub {
if (checkEvents())
{
Info ("Sending $evt_str to all websocket clients\n");
my ($serv) = @_;
$_->send_utf8($evt_str) for $serv->connections;
}
},
on_connect => sub {
my ($serv, $conn) = @_;
Info ("got a websocket connection from ".$conn->ip()."\n");
$conn->on(
utf8 => sub {
Debug ("got a message\n");
my ($conn, $msg) = @_;
$conn->send_utf8("FFS"); # In future we can support special requests from clients
},
);
}
)->start;
}