Yggdrasil Network Explorer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

157 lines
4.3 KiB

<?php
// Stop crawler on cli running
$semaphore = sem_get(crc32('yggstate.cli.yggstate'), 1);
if (false === sem_acquire($semaphore, true)) {
exit (PHP_EOL . 'yggstate.cli.yggstate process running in another thread.' . PHP_EOL);
}
// Lock multi-thread execution
$semaphore = sem_get(crc32('yggstate.crontab.crawler'), 1);
if (false === sem_acquire($semaphore, true)) {
exit (PHP_EOL . 'yggstate.crontab.crawler process locked by another thread.' . PHP_EOL);
}
// Load system dependencies
require_once(__DIR__ . '/../config/app.php');
require_once(__DIR__ . '/../library/yggdrasil.php');
require_once(__DIR__ . '/../library/mysql.php');
require_once(__DIR__ . '/../library/url.php');
// Check disk quota
if (CRAWL_STOP_DISK_QUOTA_MB_LEFT > disk_free_space('/') / 1000000) {
exit (PHP_EOL . 'Disk quota reached.' . PHP_EOL);
}
// Init Debug
$debug = [
'time' => [
'ISO8601' => date('c'),
'total' => microtime(true),
],
'yggdrasil' => [
'peer' => [
'total' => [
'online' => 0,
'insert' => 0,
],
'remote' => [
'total' => [
'insert' => 0,
'update' => 0,
]
]
]
]
];
// Connect database
try {
$db = new MySQL(DB_HOST, DB_PORT, DB_NAME, DB_USERNAME, DB_PASSWORD);
} catch(Exception $e) {
var_dump($e);
exit;
}
// Collect connected peers
if ($connectedPeers = Yggdrasil::getPeers()) {
foreach ($connectedPeers as $connectedPeerAddress => $connectedPeerInfo) {
try {
$db->beginTransaction();
// Init peer
if ($dbPeer = $db->findPeer($connectedPeerAddress)) {
$dbPeerId = $dbPeer->peerId;
} else {
$dbPeerId = $db->addPeer($connectedPeerAddress, $connectedPeerInfo->key, time());
$debug['yggdrasil']['peer']['total']['insert']++;
}
// Init peer data
if ($connectedPeerRemoteUrl = URL::parse($connectedPeerInfo->remote)) {
if ($dbPeerRemote = $db->findPeerRemote($dbPeerId,
$connectedPeerRemoteUrl->host->scheme,
$connectedPeerRemoteUrl->host->name,
$connectedPeerRemoteUrl->host->port)) {
// Update connection stats
if ($dbPeerRemote->received < $connectedPeerInfo->bytes_recvd) {
$debug['yggdrasil']['peer']['remote']['total']['update'] +=
$db->updatePeerRemoteReceived($dbPeerRemote->peerRemoteId, $connectedPeerInfo->bytes_recvd, time());
}
if ($dbPeerRemote->sent < $connectedPeerInfo->bytes_sent) {
$debug['yggdrasil']['peer']['remote']['total']['update'] +=
$db->updatePeerRemoteSent($dbPeerRemote->peerRemoteId, $connectedPeerInfo->bytes_sent, time());
}
if ($dbPeerRemote->uptime < $connectedPeerInfo->uptime) {
$debug['yggdrasil']['peer']['remote']['total']['update'] +=
$db->updatePeerRemoteUptime($dbPeerRemote->peerRemoteId, $connectedPeerInfo->uptime, time());
}
} else {
$peerRemoteId = $db->addPeerRemote($dbPeerId,
$connectedPeerRemoteUrl->host->scheme,
$connectedPeerRemoteUrl->host->name,
$connectedPeerRemoteUrl->host->port,
$connectedPeerInfo->bytes_recvd,
$connectedPeerInfo->bytes_sent,
$connectedPeerInfo->uptime,
time());
$debug['yggdrasil']['peer']['remote']['total']['insert']++;
}
}
$debug['yggdrasil']['peer']['total']['online']++;
$db->commit();
} catch(Exception $e) {
$db->rollBack();
var_dump($e);
break;
}
}
}
// Debug output
$debug['time']['total'] = microtime(true) - $debug['time']['total'];
print_r(
array_merge($debug, [
'db' => [
'total' => [
'select' => $db->getDebug()->query->select->total,
'insert' => $db->getDebug()->query->insert->total,
'update' => $db->getDebug()->query->update->total,
'delete' => $db->getDebug()->query->delete->total,
]
]
])
);