1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Popstas\Transmission\Console\Command; |
4
|
|
|
|
5
|
|
|
use InfluxDB; |
6
|
|
|
use Martial\Transmission\API\Argument\Torrent; |
7
|
|
|
use Popstas\Transmission\Console\Helpers\TorrentListUtils; |
8
|
|
|
use Popstas\Transmission\Console\Helpers\TorrentUtils; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
class StatsSend extends Command |
14
|
|
|
{ |
15
|
|
|
protected function configure() |
16
|
|
|
{ |
17
|
|
|
parent::configure(); |
18
|
|
|
$this |
19
|
|
|
->setName('stats-send') |
20
|
|
|
->setAliases(['ss']) |
21
|
|
|
->setDescription('Send metrics to InfluxDB') |
22
|
|
|
->addOption('transmission-host', null, InputOption::VALUE_OPTIONAL, 'Transmission host') |
23
|
|
|
->setHelp(<<<EOT |
24
|
|
|
## Send statistic to InfluxDB |
25
|
|
|
|
26
|
|
|
Command should called every hour or more, possible every day, but it will less precise. |
27
|
|
|
|
28
|
|
|
Command assume that all torrents have unique names. |
29
|
|
|
|
30
|
|
|
You should to configure InfluxDB to use this functional. |
31
|
|
|
EOT |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
36
|
|
|
{ |
37
|
|
|
$config = $this->getApplication()->getConfig(); |
38
|
|
|
$logger = $this->getApplication()->getLogger(); |
39
|
|
|
$client = $this->getApplication()->getClient(); |
40
|
|
|
|
41
|
|
|
$torrentList = $client->getTorrentData(); |
42
|
|
|
if (!$config->get('allow-duplicates')) { |
43
|
|
|
$obsoleteList = TorrentListUtils::getObsoleteTorrents($torrentList); |
44
|
|
|
if (!empty($obsoleteList)) { |
45
|
|
|
$output->writeln('<comment>Found obsolete torrents, ' |
46
|
|
|
. 'remove it using transmission-cli torrent-remove-duplicates</comment>'); |
47
|
|
|
TorrentListUtils::printTorrentsTable($obsoleteList, $output); |
48
|
|
|
return 1; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
try { |
53
|
|
|
$influxDbClient = $this->getApplication()->getInfluxDbClient( |
54
|
|
|
$config->get('influxdb-host'), |
55
|
|
|
$config->get('influxdb-port'), |
56
|
|
|
$config->get('influxdb-user'), |
57
|
|
|
$config->get('influxdb-password'), |
58
|
|
|
$config->get('influxdb-database') |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
$points = []; |
62
|
|
|
|
63
|
|
|
$transmissionHost = $config->get('transmission-host'); |
64
|
|
|
|
65
|
|
|
foreach ($torrentList as $torrent) { |
66
|
|
|
$age = TorrentUtils::getTorrentAge($torrent); |
67
|
|
|
$torrentPoint = $influxDbClient->buildPoint($torrent, $transmissionHost); |
68
|
|
|
|
69
|
|
|
if ($age) { |
70
|
|
|
$points[] = $torrentPoint; |
71
|
|
|
} else { |
72
|
|
|
$logger->debug('Skip point: {point}', ['point' => $torrentPoint]); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$points[] = $influxDbClient->buildStatus($torrentList, $transmissionHost); |
77
|
|
|
|
78
|
|
|
$this->dryRun($input, $output, function () use ($influxDbClient, $points) { |
79
|
|
|
$influxDbClient->writePoints($points); |
80
|
|
|
}, 'dry-run, don\'t really send points'); |
81
|
|
|
} catch (\Exception $e) { |
82
|
|
|
$logger->critical($e->getMessage()); |
83
|
|
|
return 1; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return 0; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|