Repository navigation
Expand file tree
/
Copy pathora.php
More file actions
125 lines (106 loc) · 3.02 KB
/
Copy pathora.php
File metadata and controls
125 lines (106 loc) · 3.02 KB
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
<?php
function load_config($config_location, $config)
{
if (!is_file($config_location)) {
die('[ERROR] Config file does not exist, try creating one with `php ora.php config <editor>`' . PHP_EOL);
}
$data = json_decode(file_get_contents($config_location), true);
$keys = array_keys($data);
$skeleton = array_keys($config);
foreach ($skeleton as $key) {
if (!in_array($key, $keys)) {
die('[ERROR] Config file does not contain ' . $key . PHP_EOL);
}
}
return $data;
}
function create_config($editor, $config_location, $config)
{
file_put_contents($config_location, json_encode($config, JSON_PRETTY_PRINT));
echo '[NOTE] Config file created, openning in ' . $editor;
sleep(3);
system($editor . ' ' . $config_location . '> `tty`');
}
function send_slack_message($token, $channel, $text)
{
$curl = curl_init('https://slack.com/api/chat.postMessage');
curl_setopt_array($curl, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer' . $token,
'Content-Type: \'application/json\'',
],
CURLOPT_POSTFIELDS => json_encode([
'channel' => $channel,
'text' => $text
]),
]);
curl_exec($curl);
curl_close($curl);
}
function check($partition, $max)
{
exec('df -h', $df);
foreach ($df as $line) {
$values = array_filter(explode(' ', $line), fn($item) => $item != '');
$values = array_values($values);
if ($values[0] == $partition) {
if ($values[4] > $max) {
return $values[4];
}
}
}
return false;
}
function help()
{
return 'Help
- config <editor> creates config
- --help - shows this
-h
';
}
function message($text, $percentage)
{
return str_replace('<percentage>', $percentage, $text);
}
function main($arguments)
{
$config = [
'partition' => '',
'max-usage' => 90,
'slack-channel-name' => '',
'slack-token' => '',
'slack-message' => 'Disc usage is <percentage>! <@coolfido>'
];
$config_location = __DIR__ . '/ora.json';
if (isset($arguments[1])) {
switch ($arguments[1]) {
case '--help':
case '-h':
echo help();
break;
case 'config':
create_config(
isset($arguments[2]) ? $arguments[2] : 'vim',
$config_location,
$config
);
break;
default:
echo '[ERROR] Command not found!' . PHP_EOL;
help();
break;
}
return;
}
$config = load_config($config_location, $config);
if ($percentage = check($config['partition'], $config['max-usage'])) {
send_slack_message(
$config['slack-token'],
$config['slack-channel-name'],
message($config['slack-message'], $percentage)
);
}
}
main($argv);