-
Notifications
You must be signed in to change notification settings - Fork 9
/
checkdelegate.php
executable file
·204 lines (145 loc) · 7.21 KB
/
checkdelegate.php
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
<?php
/**
* @author Jan
* @link https://github.com/lepetitjan/shift-checker
* @license https://github.com/lepetitjan/shift-checker/blob/master/LICENSE
*/
/* GENERAL SETTINGS
____________________ */
// You NEED to edit this value before running the script...
$homeDir = "/home/lepetitjan/"; // Full path to your home folder
// You may leave the settings below as they are...
$date = date("Y-m-d H:i:s"); // Current date
$pathtoapp = $homeDir."shift/"; // Full path to your shift installation
$baseDir = dirname(__FILE__)."/"; // Folder which contains THIS file
$lockfile = $baseDir."checkdelegate.lock"; // Name of our lock file
$database = $baseDir."check_fork.sqlite3"; // Database name to use
$table = "forks"; // Table name to use
$msg = "Failed to find common block with"; // Message that is printed when forked
$shiftlog = $pathtoapp."logs/shift.log"; // Needs to be a FULL path, so not ~/shift
$linestoread = 50; // How many lines to read from the end of $shiftlog
$max_count = 10; // How may times $msg may occur
// Snapshot settings
$snapshotDir = $homeDir."shift-snapshot/"; // Base folder of shift-snapshot
$createsnapshot = true; // Do you want to create daily snapshots?
$max_snapshots = 3; // How many snapshots to preserve? (in days)
// Log file rotation
$logfile = $baseDir."logs/checkdelegate.log"; // The location of your log file (see section crontab on Github)
$max_logfiles = 3; // How many log files to preserve? (in days)
$logsize = 10485760; // Max file size, default is 10 MB
/* PREREQUISITES
____________________ */
require('functions.php');
// Let's start the output with a line for the log file
echo "___________________________________________________\n";
/* LOCK FILE
____________________ */
// Check if lock file exists
if (file_exists($baseDir.$lockfile)) {
// Check age of lock file and touch it if older than 10 minutes
if((time()-filectime($baseDir.$lockfile)) >= 600){
echo $date." - [ LOCKFILE ] Lock file is older than 10 minutes. Going to touch it and continue..\n";
if (!touch($lockfile)){
exit("[ LOCKFILE ] Error touching $baseDir.$lockfile\n");
}
// If file is younger than 10 minutes, exit!
}else{
exit("[ LOCKFILE ] A previous job is still running...\n");
}
}
/* CHECK STATUS
____________________ */
echo $date." - [ STATUS ] Let's check if our delegate is still running...\n";
// Check status with shift_manager.bash. Use PHP's ob_ function to create an output buffer
ob_start();
$check_status = passthru("cd $pathtoapp && bash shift_manager.bash status | cut -z -b1-3");
$check_output = ob_get_contents();
ob_end_clean();
// If status is not OK...
if(strpos($check_output, 'OK') === false){
// Echo something to our log file
echo $date." - [ STATUS ] Delegate not running/healthy. Let me restart it for you...\n";
echo $date." - [ STATUS ] Stopping all forever processes...\n";
passthru("forever stopall");
echo $date." - [ STATUS ] Starting Shift forever proces...\n";
passthru("cd $pathtoapp && forever start app.js");
}else{
echo $date." - [ STATUS ] Delegate is still running...\n";
}
/* CHECK IF FORKED
____________________ */
echo $date." - [ FORKING ] Going to check for forked status now...\n";
// Set the database to save our counts to
$db = new SQLite3($database) or die("[ FORKING ] Unable to open database");
// Create table if not exists
$db->exec("CREATE TABLE IF NOT EXISTS $table (
id INTEGER PRIMARY KEY,
counter INTEGER,
time INTEGER)");
// Let's check if any rows exists in our table
$check_exists = $db->query("SELECT count(*) AS count FROM $table");
$row_exists = $check_exists->fetchArray();
$numExists = $row_exists['count'];
// If no rows exist in our table, add one
if($numExists < 1){
// Echo something to our log file
echo $date." - [ FORKING ] No rows exist in our table to update the counter...Adding a row for you.\n";
$insert = "INSERT INTO $table (counter, time) VALUES ('0', time())";
$db->exec($insert) or die("[ FORKING ] Failed to add row!");
}
// Tail shift.log
$last = tailCustom($shiftlog, $linestoread);
// Count how many times the fork message appears in the tail
$count = substr_count($last, $msg);
// Get counter value from our database
$check_count = $db->query("SELECT * FROM $table LIMIT 1");
$row = $check_count->fetchArray();
$counter = $row['counter'];
// If counter + current count is greater than $max_count, take action...
if (($counter + $count) >= $max_count) {
echo $date." - [ FORKING ] Hit max_count. I am going to restore from a snapshot.\n";
passthru("cd $pathtoapp && forever stop app.js");
passthru("cd $snapshotDir && echo y | ./shift-snapshot.sh restore");
passthru("cd $pathtoapp && forever start app.js");
echo $date." - [ FORKING ] Finally, I will reset the counter for you...\n";
$query = "UPDATE $table SET counter='0', time=time()";
$db->exec($query) or die("[ FORKING ] Unable to set counter to 0!");
// If counter + current count is not greater than $max_count, add current count to our database...
} else {
$query = "UPDATE $table SET counter=counter+$count, time=time()";
$db->exec($query) or die("[ FORKING ] Unable to plus the counter!");
echo $date." - [ FORKING ] Counter ($counter) + current count ($count) is not sufficient to restore from snapshot. Need: $max_count \n";
// Check snapshot setting
if($createsnapshot === false){
echo $date." - [ SNAPSHOT ] Snapshot setting is disabled.\n";
}
// If counter + current count equals 0 AND option $createsnapshot is true, create a new snapshot
if(($counter + $count) == 0 && $createsnapshot === true){
echo $date." - [ SNAPSHOT ] It's safe to create a daily snapshot and the setting is enabled.\n";
echo $date." - [ SNAPSHOT ] Let's check if a snapshot was already created today...\n";
$snapshots = glob($snapshotDir.'snapshot/shift_db'.date("d-m-Y").'*.snapshot.tar');
if (!empty($snapshots)) {
echo $date." - [ SNAPSHOT ] A snapshot for today already exists:\n";
print_r($snapshots)."\n";
echo $date." - [ SNAPSHOT ] Going to remove snapshots older than $max_snapshots days...\n";
$files = glob($snapshotDir.'snapshot/shift_db*.snapshot.tar');
foreach($files as $file){
if(is_file($file)){
if(time() - filemtime($file) >= 60 * 60 * 24 * $max_snapshots){
if(unlink($file)){
echo $date." - [ SNAPSHOT ] Deleted snapshot $file\n";
}
}
}
}
echo $date." - [ SNAPSHOT ] Done!\n";
}else{
echo $date." - [ SNAPSHOT ] No snapshot exists for today, I will create one for you now!\n";
passthru("cd $snapshotDir && ./shift-snapshot.sh create");
echo $date." - [ SNAPSHOT ] Done!\n";
}
}
}
// Cleaning up your log file(s)
echo $date." - [ LOGFILES ] Performing log rotation and cleanup...\n";
rotateLog($logfile, $max_logfiles, $logsize);