| 1 | <?php
|
|---|
| 2 |
|
|---|
| 3 | class PageNetworkTopology extends Page
|
|---|
| 4 | {
|
|---|
| 5 | public string $TopHostName = 'NIX-ROUTER';
|
|---|
| 6 |
|
|---|
| 7 | function __construct(System $System)
|
|---|
| 8 | {
|
|---|
| 9 | parent::__construct($System);
|
|---|
| 10 | $this->Title = 'Topologie sítě';
|
|---|
| 11 | $this->Description = 'Grafické zobrazení topologie sítě';
|
|---|
| 12 | $this->ParentClass = 'PagePortal';
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 15 | function Show(): string
|
|---|
| 16 | {
|
|---|
| 17 | if (count($this->System->PathItems) > 1)
|
|---|
| 18 | {
|
|---|
| 19 | if ($this->System->PathItems[1] == 'topologie.png') return $this->ShowImage();
|
|---|
| 20 | else return PAGE_NOT_FOUND;
|
|---|
| 21 |
|
|---|
| 22 | } else return $this->ShowOverview();
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | function ShowImage(): string
|
|---|
| 26 | {
|
|---|
| 27 | $this->RawPage = true;
|
|---|
| 28 | Core::Cast($this->System)->BaseView->FormatHTML = false;
|
|---|
| 29 |
|
|---|
| 30 | if (array_key_exists('debug', $_GET)) $debug = $_GET['debug'];
|
|---|
| 31 | else $debug = 0;
|
|---|
| 32 |
|
|---|
| 33 | // Vytvoř stromy a ulož výsledek do databáze
|
|---|
| 34 | $DbResult = $this->Database->query("SELECT MAX(Pos), MAX(Depth) FROM NetworkTopology");
|
|---|
| 35 | $item = $DbResult->fetch_array();
|
|---|
| 36 | $width = $item[0];
|
|---|
| 37 | $height = $item[1];
|
|---|
| 38 |
|
|---|
| 39 | $spacex = 32;
|
|---|
| 40 | $spacey = 68;
|
|---|
| 41 | $halfx = $spacex / 2;
|
|---|
| 42 | $halfy = $spacey / 2;
|
|---|
| 43 | // Načtení pomocných obrázků
|
|---|
| 44 | $im_comp = imagecreatefrompng(dirname(__FILE__).'/images/comp.png');
|
|---|
| 45 | $im_dev = imagecreatefrompng(dirname(__FILE__).'/images/device.png');
|
|---|
| 46 | // Generování obrázku
|
|---|
| 47 | $im = imagecreate(($width + 1.6) * $spacex, ($height + 1) * $spacey);
|
|---|
| 48 | $background_color = imagecolorallocate($im, 255, 255, 255);
|
|---|
| 49 | $black = imagecolorallocate($im, 0, 0, 0);
|
|---|
| 50 | $red = imagecolorallocate($im, 255, 0, 0);
|
|---|
| 51 | $green = imagecolorallocate($im, 0, 128, 0);
|
|---|
| 52 | $blue = imagecolorallocate($im, 100, 100, 255);
|
|---|
| 53 | $gray = imagecolorallocate($im, 160, 160, 160);
|
|---|
| 54 |
|
|---|
| 55 | //$DbResult = $Database->query("SELECT * FROM hosts, NetworkTopology WHERE host = id AND used=1");
|
|---|
| 56 | $DbResult = $this->Database->query('SELECT NetworkTopology.*, NetworkTopology.Host AS Id, '.
|
|---|
| 57 | 'NetworkDevice.Name AS Name, '.
|
|---|
| 58 | 'NetworkDevice.Online AS Online, NetworkDeviceType.IconName AS IconName, 1 AS ShowOnline '.
|
|---|
| 59 | 'FROM NetworkTopology LEFT JOIN NetworkDevice ON NetworkDevice.Id = NetworkTopology.Host '.
|
|---|
| 60 | 'LEFT JOIN NetworkDeviceType ON NetworkDevice.Type = NetworkDeviceType.Id');
|
|---|
| 61 | while ($item = $DbResult->fetch_array())
|
|---|
| 62 | {
|
|---|
| 63 | $id = $item['Id'];
|
|---|
| 64 | $vleft = $item['Pos'];
|
|---|
| 65 | $vtop = $item['Depth'];
|
|---|
| 66 | $vfirst = $item['First'];
|
|---|
| 67 | $vlast = $item['Last'];
|
|---|
| 68 | $xpos = $vleft * $spacex;
|
|---|
| 69 | if (($vtop > 0) or ($item['Name'] == $this->TopHostName))
|
|---|
| 70 | {
|
|---|
| 71 | if ($vtop > 0) imageline($im, $xpos + $halfx, $vtop * $spacey, $xpos + $halfx, $vtop * $spacey + 8, $black);
|
|---|
| 72 | if ($vfirst >= 0)
|
|---|
| 73 | {
|
|---|
| 74 | imageline($im, $vfirst*$spacex + $halfx, $vtop * $spacey + $spacey, $vlast*$spacex + $halfx, $vtop * $spacey + $spacey, $black);
|
|---|
| 75 | imageline($im, $xpos + $halfx, ($vtop + 1) * $spacey - 10, $xpos + $halfx, ($vtop + 1) * $spacey, $black);
|
|---|
| 76 | }
|
|---|
| 77 | // $ip = explode('.',$item['IP']);
|
|---|
| 78 | // if (!array_key_exists(3, $ip)) $ip[3] = '';
|
|---|
| 79 | if ($item['IconName'] == 'comp')
|
|---|
| 80 | {
|
|---|
| 81 | if ($item['Online'] == 1) $color = $green;
|
|---|
| 82 | else $color = $black;
|
|---|
| 83 | $image = $im_comp;
|
|---|
| 84 | } else $image = $im_dev;
|
|---|
| 85 | if ($item['IconName'] == 'device')
|
|---|
| 86 | {
|
|---|
| 87 | if ($item['Online'] == 1) $color = $green;
|
|---|
| 88 | else $color = $red;
|
|---|
| 89 | $image = $im_dev;
|
|---|
| 90 | }
|
|---|
| 91 | if ($item['ShowOnline'] == 0)
|
|---|
| 92 | {
|
|---|
| 93 | $color = $gray;
|
|---|
| 94 | $image = $im_dev;
|
|---|
| 95 | }
|
|---|
| 96 | // $text='IP: '.$ip[0];
|
|---|
| 97 | // imagestring($im,2,xpos($id)+($spacex-strlen($text)*imagefontwidth(2))/2,$vtop[$id]*$spacey+24+imagefontheight(2),$text,$black);
|
|---|
| 98 | imagecopy($im, $image, $xpos+$halfx-15, $vtop*$spacey+12, 0, 0, 30, 30);
|
|---|
| 99 | // imagerectangle($im,xpos($id)+$halfx-6,$vtop[$id]*$spacey+16,xpos($id)+$halfx+6,$vtop[$id]*$spacey+28,$color);
|
|---|
| 100 | if ($debug) {
|
|---|
| 101 | imagestring($im, 2, $xpos+($spacex-strlen($item['Id'])*imagefontwidth(2))/2, $vtop*$spacey+31+imagefontheight(2), $item['Id'], $color);
|
|---|
| 102 | } else imagestring($im,2,$xpos+($spacex-strlen($item['Name'])*imagefontwidth(2))/2, $vtop*$spacey+31+imagefontheight(2), $item['Name'], $color);
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | // === Sestavení výsledného souboru ============================================
|
|---|
| 107 | if ($debug == 0)
|
|---|
| 108 | {
|
|---|
| 109 | Header("Content-type: image/png");
|
|---|
| 110 | Header("Cache-Control: no-cache"); // Dynamický graf, nekešovat
|
|---|
| 111 | imagepng($im);
|
|---|
| 112 | imagedestroy($im);
|
|---|
| 113 | imagedestroy($im_comp);
|
|---|
| 114 | imagedestroy($im_dev);
|
|---|
| 115 | }
|
|---|
| 116 | return '';
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | function ShowOverview(): string
|
|---|
| 120 | {
|
|---|
| 121 | $Output = '<img src="topologie.png"><br/>
|
|---|
| 122 | <p>Vysvětlivky: Obrázek znázorňuje připojení prvků sítě do internetu.<br/>
|
|---|
| 123 | <img src="Modules/NetworkTopology/images/comp.png"> Je počítač zapojený v síti a pod ním je uvedeno jeho jméno.
|
|---|
| 124 | <span style="color:green">Zelená barva</span> znamená, že počitač je zapnutý a
|
|---|
| 125 | online. Černá barva znamená, že počítač je vypnutý, nebo není online.<br/>
|
|---|
| 126 | <img src="Modules/NetworkTopology/images/device.png"> Představuje komunikační zařízení připojené do sítě, které
|
|---|
| 127 | mají běžet nepřetržitě. Pokud je zařízení online, je text pod ním zobrazen
|
|---|
| 128 | <span style="color:green">zelenou barvou</span>, pokud není online, je zobrazen
|
|---|
| 129 | <span style="color:red">červenou barvou</span>. Dostupnost prvků je kontrolována
|
|---|
| 130 | z počítače Centrála, takže při výpadku zařízení nebude možné zjistit stav
|
|---|
| 131 | zařízení, které jsou přes něj připojeny. U zařízení, kde není stav možné
|
|---|
| 132 | zjišťovat je použita <span style="color:gray">šedá barvou</span>.</p>';
|
|---|
| 133 | return $Output;
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | class ModuleNetworkTopology extends Module
|
|---|
| 138 | {
|
|---|
| 139 | function __construct(System $System)
|
|---|
| 140 | {
|
|---|
| 141 | parent::__construct($System);
|
|---|
| 142 | $this->Name = 'NetworkTopology';
|
|---|
| 143 | $this->Version = '1.0';
|
|---|
| 144 | $this->Creator = 'Chronos';
|
|---|
| 145 | $this->License = 'GNU/GPLv3';
|
|---|
| 146 | $this->Description = 'Generate image of network device interconnection';
|
|---|
| 147 | $this->Dependencies = array(ModuleNetwork::GetName());
|
|---|
| 148 | $this->Models = array(NetworkTopology::GetClassName());
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | function DoStart(): void
|
|---|
| 152 | {
|
|---|
| 153 | $this->System->RegisterPage(['topologie'], 'PageNetworkTopology');
|
|---|
| 154 | }
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | class NetworkTopology extends Model
|
|---|
| 158 | {
|
|---|
| 159 | static function GetModelDesc(): ModelDesc
|
|---|
| 160 | {
|
|---|
| 161 | $Desc = new ModelDesc(self::GetClassName());
|
|---|
| 162 | $Desc->AddInteger('Host');
|
|---|
| 163 | $Desc->AddInteger('Depth');
|
|---|
| 164 | $Desc->AddInteger('Pos');
|
|---|
| 165 | $Desc->AddInteger('First');
|
|---|
| 166 | $Desc->AddInteger('Last');
|
|---|
| 167 | return $Desc;
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|