Completed
Push — dev ( 4b146c...f49bf6 )
by Darko
07:46
created

window_sharing()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
require_once dirname(__DIR__, 3).DIRECTORY_SEPARATOR.'bootstrap/autoload.php';
4
5
use App\Models\Collection;
6
use App\Models\Settings;
7
use Blacklight\ColorCLI;
8
use Blacklight\utility\Utility;
9
use Illuminate\Support\Facades\DB;
10
11
$DIR = NN_TMUX;
12
$import = Settings::settingValue('site.tmux.import') ?? 0;
13
$tmux_session = Settings::settingValue('site.tmux.tmux_session') ?? 0;
14
$seq = Settings::settingValue('site.tmux.sequential') ?? 0;
15
$delaytimet = Settings::settingValue('..delaytime');
16
$delaytimet = $delaytimet ? (int) $delaytimet : 2;
0 ignored issues
show
introduced by
$delaytimet is of type App\Models\Settings, thus it always evaluated to true.
Loading history...
17
$colorCli = new ColorCLI();
18
19
Utility::clearScreen();
20
21
echo 'Starting Tmux...'.PHP_EOL;
22
23
//check if session exists
24
$session = shell_exec("tmux list-session | grep $tmux_session");
25
if ($session !== null) {
26
    $colorCli->error("tmux session: '".$tmux_session."' is already running, aborting.");
27
    exit();
28
}
29
30
//reset collections dateadded to now if dateadded > delay time check
31
$colorCli->header('Resetting expired collections dateadded to now. This could take some time if many collections need to be reset');
32
33
Collection::query()->where('dateadded', '<', now()->subHours($delaytimet))->update(['dateadded' => now()]);
34
35
function writelog($pane)
36
{
37
    $path = NN_RES.'logs';
38
    $getdate = gmdate('Ymd');
39
    $logs = Settings::settingValue('site.tmux.write_logs');
40
    if ((int) $logs === 1) {
41
        return "2>&1 | tee -a $path/$pane-$getdate.log";
42
    }
43
44
    return '';
45
}
46
47
function command_exist($cmd)
48
{
49
    $returnVal = exec("which $cmd 2>/dev/null");
50
51
    return ! empty($returnVal);
52
}
53
54
//check for apps
55
$apps = ['time', 'tmux', 'nice', 'tee'];
56
foreach ($apps as &$value) {
57
    if (! command_exist($value)) {
58
        $colorCli->error('Tmux scripts require '.$value.' but its not installed. Aborting.');
59
        exit();
60
    }
61
}
62
63
unset($value);
64
65
function start_apps($tmux_session)
66
{
67
    $htop = Settings::settingValue('site.tmux.htop');
68
    $vnstat = Settings::settingValue('site.tmux.vnstat');
69
    $vnstat_args = Settings::settingValue('site.tmux.vnstat_args');
70
    $tcptrack = Settings::settingValue('site.tmux.tcptrack');
71
    $tcptrack_args = Settings::settingValue('site.tmux.tcptrack_args');
72
    $nmon = Settings::settingValue('site.tmux.nmon');
73
    $bwmng = Settings::settingValue('site.tmux.bwmng');
74
    $mytop = Settings::settingValue('site.tmux.mytop');
75
    $redis = Settings::settingValue('site.tmux.redis');
76
    $showprocesslist = Settings::settingValue('site.tmux.showprocesslist');
77
    $processupdate = Settings::settingValue('site.tmux.processupdate');
78
    $console_bash = Settings::settingValue('site.tmux.console');
79
80
    if ((int) $htop === 1 && command_exist('htop')) {
81
        exec("tmux new-window -t $tmux_session -n htop 'printf \"\033]2;htop\033\" && htop'");
82
    }
83
84
    if ((int) $nmon === 1 && command_exist('nmon')) {
85
        exec("tmux new-window -t $tmux_session -n nmon 'printf \"\033]2;nmon\033\" && nmon -t'");
86
    }
87
88
    if ((int) $vnstat === 1 && command_exist('vnstat')) {
89
        exec("tmux new-window -t $tmux_session -n vnstat 'printf \"\033]2;vnstat\033\" && watch -n10 \"vnstat ${vnstat_args}\"'");
90
    }
91
92
    if ((int) $tcptrack === 1 && command_exist('tcptrack')) {
93
        exec("tmux new-window -t $tmux_session -n tcptrack 'printf \"\033]2;tcptrack\033\" && tcptrack ${tcptrack_args}'");
94
    }
95
96
    if ((int) $bwmng === 1 && command_exist('bwm-ng')) {
97
        exec("tmux new-window -t $tmux_session -n bwm-ng 'printf \"\033]2;bwm-ng\033\" && bwm-ng'");
98
    }
99
100
    if ((int) $mytop === 1 && command_exist('mytop')) {
101
        exec("tmux new-window -t $tmux_session -n mytop 'printf \"\033]2;mytop\033\" && mytop -u'");
102
    }
103
104
    if ((int) $redis === 1 && command_exist('redis-cli')) {
105
        exec("tmux new-window -t $tmux_session -n redis-stat 'printf \"\033]2;redis-stat\033\" && redis-stat --verbose --server=63790'");
106
    }
107
108
    if ((int) $showprocesslist === 1) {
109
        exec("tmux new-window -t $tmux_session -n showprocesslist 'printf \"\033]2;showprocesslist\033\" && watch -n .5 \"mysql -e \\\"SELECT time, state, info FROM information_schema.processlist WHERE command != \\\\\\\"Sleep\\\\\\\" AND time >= $processupdate ORDER BY time DESC \\\G\\\"\"'");
110
    }
111
112
    if ((int) $console_bash === 1) {
113
        exec("tmux new-window -t $tmux_session -n bash 'printf \"\033]2;Bash\033\" && bash -i'");
114
    }
115
}
116
117
function window_utilities($tmux_session)
118
{
119
    exec("tmux new-window -t $tmux_session -n utils 'printf \"\033]2;fixReleaseNames\033\"'");
120
    exec("tmux splitw -t $tmux_session:1 -v -p 50 'printf \"\033]2;updateTVandTheaters\033\"'");
121
    exec("tmux selectp -t $tmux_session:1.0; tmux splitw -t $tmux_session:1 -h -p 50 'printf \"\033]2;removeCrapReleases\033\"'");
122
    exec("tmux selectp -t $tmux_session:1.2; tmux splitw -t $tmux_session:1 -h -p 50 'printf \"\033]2;decryptHashes\033\"'");
123
}
124
125
function window_stripped_utilities($tmux_session)
126
{
127
    exec("tmux new-window -t $tmux_session -n utils 'printf \"\033]2;updateTVandTheaters\033\"'");
128
    exec("tmux selectp -t $tmux_session:1.0; tmux splitw -t $tmux_session:1 -h -p 50 'printf \"\033]2;postprocessing_amazon\033\"'");
129
}
130
131
function window_ircscraper($tmux_session)
132
{
133
    exec("tmux new-window -t $tmux_session -n IRCScraper 'printf \"\033]2;scrapeIRC\033\"'");
134
}
135
136
function window_post($tmux_session)
137
{
138
    exec("tmux new-window -t $tmux_session -n post 'printf \"\033]2;postprocessing_additional\033\"'");
139
    exec("tmux splitw -t $tmux_session:2 -v -p 67 'printf \"\033]2;postprocessing_non_amazon\033\"'");
140
    exec("tmux splitw -t $tmux_session:2 -v -p 50 'printf \"\033]2;postprocessing_amazon\033\"'");
141
}
142
143
function window_optimize($tmux_session)
144
{
145
    exec("tmux new-window -t $tmux_session -n optimize 'printf \"\033]2;update_Tmux\033\"'");
146
    exec("tmux splitw -t $tmux_session:3 -v -p 50 'printf \"\033]2;optimize\033\"'");
147
}
148
149
function attach($DIR, $tmux_session)
150
{
151
    exec("tmux respawnp -t $tmux_session:0.0 'php ".$DIR."monitor.php'");
152
    exec("tmux select-window -t $tmux_session:0; tmux attach-session -d -t $tmux_session");
153
}
154
155
//create tmux session
156
157
$tmuxconfig = $DIR.'tmux.conf';
158
159
if ((int) $seq === 1) {
160
    exec("cd ${DIR}; tmux -f $tmuxconfig new-session -d -s $tmux_session -n Monitor 'printf \"\033]2;\"Monitor\"\033\"'");
161
    exec("tmux selectp -t $tmux_session:0.0; tmux splitw -t $tmux_session:0 -h -p 67 'printf \"\033]2;update_releases\033\"'");
162
    exec("tmux selectp -t $tmux_session:0.0; tmux splitw -t $tmux_session:0 -v -p 25 'printf \"\033]2;nzb-import\033\"'");
163
164
    window_utilities($tmux_session);
165
    window_post($tmux_session);
166
    window_ircscraper($tmux_session);
167
    start_apps($tmux_session);
168
    attach($DIR, $tmux_session);
169
} elseif ((int) $seq === 2) {
170
    exec("cd ${DIR}; tmux -f $tmuxconfig new-session -d -s $tmux_session -n Monitor 'printf \"\033]2;\"Monitor\"\033\"'");
171
    exec("tmux selectp -t $tmux_session:0.0; tmux splitw -t $tmux_session:0 -h -p 67 'printf \"\033]2;sequential\033\"'");
172
    exec("tmux selectp -t $tmux_session:0.0; tmux splitw -t $tmux_session:0 -v -p 25 'printf \"\033]2;nzb-import\033\"'");
173
174
    window_stripped_utilities($tmux_session);
175
    window_ircscraper($tmux_session);
176
    start_apps($tmux_session);
177
    attach($DIR, $tmux_session);
178
} else {
179
    exec("cd ${DIR}; tmux -f $tmuxconfig new-session -d -s $tmux_session -n Monitor 'printf \"\033]2;Monitor\033\"'");
180
    exec("tmux selectp -t $tmux_session:0.0; tmux splitw -t $tmux_session:0 -h -p 67 'printf \"\033]2;update_binaries\033\"'");
181
    exec("tmux selectp -t $tmux_session:0.0; tmux splitw -t $tmux_session:0 -v -p 25 'printf \"\033]2;nzb-import\033\"'");
182
    exec("tmux selectp -t $tmux_session:0.2; tmux splitw -t $tmux_session:0 -v -p 67 'printf \"\033]2;backfill\033\"'");
183
    exec("tmux splitw -t $tmux_session -v -p 50 'printf \"\033]2;update_releases\033\"'");
184
185
    window_utilities($tmux_session);
186
    window_post($tmux_session);
187
    window_ircscraper($tmux_session);
188
    start_apps($tmux_session);
189
    attach($DIR, $tmux_session);
190
}
191