/
Version.php
217 lines (185 loc) · 5.74 KB
/
Version.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
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* Version.php
*
* Get version info about LibreNMS and various components/dependencies
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*
* @link https://www.librenms.org
*
* @copyright 2019 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Util;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use LibreNMS\Config;
use LibreNMS\DB\Eloquent;
use Symfony\Component\Process\Process;
class Version
{
/** @var string Update this on release */
public const VERSION = '23.11.0';
/** @var Git convenience instance */
public $git;
public function __construct()
{
$this->git = Git::make();
}
public static function get(): Version
{
return new static;
}
public function release(): string
{
return Config::get('update_channel') == 'master' ? 'master' : self::VERSION;
}
public function date(string $format = 'c'): string
{
return date($format, $this->git->commitDate() ?: filemtime(__FILE__)); // approximate date for non-git installs
}
public function name(): string
{
return $this->git->tag() ?: self::VERSION;
}
public function databaseServer(): string
{
if (! Eloquent::isConnected()) {
return 'Not Connected';
}
switch (Eloquent::getDriver()) {
case 'mysql':
$ret = Arr::first(DB::selectOne('select version()'));
return (str_contains($ret, 'MariaDB') ? 'MariaDB ' : 'MySQL ') . $ret;
case 'sqlite':
return 'SQLite ' . Arr::first(DB::selectOne('select sqlite_version()'));
default:
return 'Unsupported: ' . Eloquent::getDriver();
}
}
/**
* Get the database last migration and count as a string
*/
public function database(): string
{
return sprintf('%s (%s)', $this->lastDatabaseMigration(), $this->databaseMigrationCount());
}
/**
* Get the total number of migrations applied to the database
*/
public function databaseMigrationCount(): int
{
try {
if (Eloquent::isConnected()) {
return Eloquent::DB()->table('migrations')->count();
}
} catch (\Exception $e) {
}
return 0;
}
/**
* Get the name of the last migration that was applied to the database
*/
public function lastDatabaseMigration(): string
{
if (! Eloquent::isConnected()) {
return 'Not Connected';
}
try {
return Eloquent::DB()->table('migrations')->orderBy('id', 'desc')->value('migration');
} catch (\Exception $e) {
return 'No Schema';
}
}
public function python(): string
{
$proc = new Process(['python3', '--version']);
$proc->run();
if ($proc->getExitCode() !== 0) {
return '';
}
return explode(' ', rtrim($proc->getOutput()), 2)[1] ?? '';
}
public function rrdtool(): string
{
$process = new Process([Config::get('rrdtool', 'rrdtool'), '--version']);
$process->run();
preg_match('/^RRDtool ([\w.]+) /', $process->getOutput(), $matches);
return str_replace('1.7.01.7.0', '1.7.0', $matches[1] ?? '');
}
public function netSnmp(): string
{
$process = new Process([Config::get('snmpget', 'snmpget'), '-V']);
$process->run();
preg_match('/[\w.]+$/', $process->getErrorOutput(), $matches);
return $matches[0] ?? '';
}
/**
* The OS/distribution and version
*/
public function os(): string
{
$info = [];
// find release file
if (file_exists('/etc/os-release')) {
$info = @parse_ini_file('/etc/os-release');
} else {
foreach (glob('/etc/*-release') as $file) {
$content = file_get_contents($file);
// normal os release style
$info = @parse_ini_string($content);
if (! empty($info)) {
break;
}
// just a string of text
if (substr_count($content, PHP_EOL) <= 1) {
$info = ['NAME' => trim(str_replace('release ', '', $content))];
break;
}
}
}
$only = array_intersect_key($info, ['NAME' => true, 'VERSION_ID' => true]);
return implode(' ', $only);
}
/**
* Get a formatted header to print out to the user.
*/
public function header(): string
{
return sprintf(<<<'EOH'
===========================================
Component | Version
--------- | -------
LibreNMS | %s (%s)
DB Schema | %s (%s)
PHP | %s
Python | %s
Database | %s
RRDTool | %s
SNMP | %s
===========================================
EOH,
$this->name(),
$this->date(),
$this->lastDatabaseMigration(),
$this->databaseMigrationCount(),
phpversion(),
$this->python(),
$this->databaseServer(),
$this->rrdtool(),
$this->netSnmp()
);
}
}