addClassPaths('Collector'); } public function getFormService(): CollectorForm { if ($this->_form === null) { $this->_form = new CollectorForm(); $this->_form->setAttributes([ 'pk_field' => $this->model->getPKField(), 'title_field' => $this->model->getTitleField(), 'table' => $this->model->getTable(), 'useAutoIncrement' => $this->model->useAutoIncrement(), 'class_path' => $this->getClassPaths(false), ]); } return $this->_form; } public function getHelper(): CollectorHelper { if ($this->_helper === null) { $this->_helper = new CollectorHelper(); $this->_helper->setAttributes([ 'pk_field' => $this->model->getPKField(), 'title_field' => $this->model->getTitleField(), 'table' => $this->model->getTable(), 'useAutoIncrement' => $this->model->useAutoIncrement(), 'class_path' => $this->getClassPaths(false), ]); } return $this->_helper; } //๊ธฐ๋ณธ ๊ธฐ๋Šฅ๋ถ€๋ถ„ public function create(object $dto): CollectorEntity { if (!$dto instanceof CollectorDTO) { throw new RuntimeException(__METHOD__ . "์—์„œ ์˜ค๋ฅ˜๋ฐœ์ƒ:" . get_class($dto) . "๋Š” ์‚ฌ์šฉํ• ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); } return parent::create($dto); } public function modify($uid, object $dto): CollectorEntity { if (!$dto instanceof CollectorDTO) { throw new RuntimeException(__METHOD__ . "์—์„œ ์˜ค๋ฅ˜๋ฐœ์ƒ:" . get_class($dto) . "๋Š” ์‚ฌ์šฉํ• ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); } return parent::modify($uid, $dto); } //List ๊ฒ€์ƒ‰์šฉ //FormFilter ์กฐ๊ฑด์ ˆ ์ฒ˜๋ฆฌ //๊ฒ€์ƒ‰์–ด์กฐ๊ฑด์ ˆ์ฒ˜๋ฆฌ public function setSearchWord(string $word): void { $this->model->orLike($this->model->getTable() . "." . $this->model->getTitleField(), $word, 'both'); parent::setSearchWord($word); } //Chart์šฉ public function getAggregateDatas(TrafficEntity $trafficEntity, string $startDate, string $endDate): array { $entities = []; foreach ($this->getEntities(["trafficinfo_uid" => $trafficEntity->getPK(), "created_at >=" => $startDate, "created_at <=" => $endDate]) as $entity) { $entities[] = ['in_kbits' => $entity->getIn(), 'out_kbits' => $entity->getOut(), 'created_at' => $entity->getCreatedAt()]; } return $entities; } //SNMP์—ฐ๊ฒฐ private function getSNMPOctets(TrafficEntity $trafficEntity, string $oid): ?int { $fullOid = $oid . $trafficEntity->getInterface(); $community = $trafficEntity->getCommunity(); $ip = $trafficEntity->getIP(); // snmp2_get์„ ์‚ฌ์šฉํ•˜์—ฌ SNMP v2c๋กœ ์š”์ฒญ // ๐Ÿ’ก snmp2_get() ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” LSP ์˜ค๋ฅ˜๋ฅผ ๋ฌด์‹œํ•˜๊ธฐ ์œ„ํ•ด @suppress ํƒœ๊ทธ ์‚ฌ์šฉ /** @phpstan-ignore-next-line */ $result = @snmp2_get($ip, $community, $fullOid, 100000, 3); if ($result === false || $result === null) { log_message('error', "SNMP ํ†ต์‹  ์‹คํŒจ: {$ip} ({$fullOid}, Community: {$community})"); return null; } // ๐Ÿ’ก ์ •๊ทœ์‹ ์ˆ˜์ •: /\d+$/ (๋ฌธ์ž์—ด ๋์˜ ์ˆซ์ž๋งŒ ์ถ”์ถœ) if (preg_match('/\d+$/', $result, $matches)) { // SNMP Counter๋Š” 64๋น„ํŠธ BigInt๊ฐ€ ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, 64๋น„ํŠธ PHP ํ™˜๊ฒฝ์—์„œ๋Š” // (int) ์บ์ŠคํŒ…์„ ์‚ฌ์šฉํ•˜์—ฌ ์•ˆ์ „ํ•˜๊ฒŒ 64๋น„ํŠธ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. // (BigInt ๊ฐ’์„ DB์— ์ €์žฅํ•  ๋•Œ๋„ PHP์˜ ์ •์ˆ˜ํ˜•/๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ ๋Šฅ๋ ฅ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.) return (int)$matches[0]; } return null; } public function getCalculatedData(TrafficEntity $trafficEntity): array { $currentInOctets = $this->getSNMPOctets($trafficEntity, self::OID_IF_IN_OCTETS); $currentOutOctets = $this->getSNMPOctets($trafficEntity, self::OID_IF_OUT_OCTETS); if ($currentInOctets === null || $currentOutOctets === null) { $message = "ํŠธ๋ž˜ํ”ฝ ์ˆ˜์ง‘ ์‹คํŒจ: {$trafficEntity->getIP()} - IF{$trafficEntity->getInterface()} (UID: {$trafficEntity->getPK()})"; log_message('warning', $message); throw new \Exception($message); } // ์ด์ „ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜์—ฌ Rate ๊ณ„์‚ฐ์— ์‚ฌ์šฉ // $this->model์€ TrafficDataModel์˜ ์ธ์Šคํ„ด์Šค๋ผ๊ณ  ๊ฐ€์ • $lastEntity = $this->model->getLastEntity($trafficEntity->getPK()); $inKbitsSec = 0.0; $outKbitsSec = 0.0; // ์ด์ „ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์–ด์•ผ๋งŒ Rate ๊ณ„์‚ฐ ๊ฐ€๋Šฅ if ($lastEntity !== null) { $lastTime = Time::parse($lastEntity->getCreatedAt())->getTimestamp(); $deltaTime = Time::now()->getTimestamp() - $lastTime; if ($deltaTime > 0) { // DB์—์„œ ๊ฐ€์ ธ์˜จ ๊ฐ’๋„ BIGINT ํƒ€์ž…์œผ๋กœ ์ž˜ ์ฒ˜๋ฆฌ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. // PHP 64๋น„ํŠธ ํ™˜๊ฒฝ์ด๋ผ๋ฉด ์ž๋™์œผ๋กœ 64๋น„ํŠธ ์ •์ˆ˜๋กœ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. $lastIn = $lastEntity->getRawIn(); $lastOut = $lastEntity->getRawOut(); // ๐Ÿ’ก 1. ์ธ๋ฐ”์šด๋“œ Octets ์ฐจ๋ถ„ ๊ณ„์‚ฐ (32๋น„ํŠธ ๋กค์˜ค๋ฒ„ ๋กœ์ง ์ œ๊ฑฐ) // 64๋น„ํŠธ ์นด์šดํ„ฐ(BIGINT)๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์ˆœ ๋บ„์…ˆ์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. // 64๋น„ํŠธ ์นด์šดํ„ฐ๋Š” ์‹ค์งˆ์ ์œผ๋กœ ๋กค์˜ค๋ฒ„๋  ์ผ์ด ์—†์Šต๋‹ˆ๋‹ค. $deltaInOctets = $currentInOctets - $lastIn; // ๐Ÿ’ก 2. ์•„์›ƒ๋ฐ”์šด๋“œ Octets ์ฐจ๋ถ„ ๊ณ„์‚ฐ (32๋น„ํŠธ ๋กค์˜ค๋ฒ„ ๋กœ์ง ์ œ๊ฑฐ) // 64๋น„ํŠธ ์นด์šดํ„ฐ(BIGINT)๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์ˆœ ๋บ„์…ˆ์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. $deltaOutOctets = $currentOutOctets - $lastOut; // Kbit/s ๊ณ„์‚ฐ: (Delta_Octets * 8 bits) / Delta_Time_Seconds / 1000 (-> Kbit/s) // ์‹ค์ˆ˜(float) ์—ฐ์‚ฐ์œผ๋กœ ์ •ํ™•๋„๋ฅผ ๋†’์ž…๋‹ˆ๋‹ค. $inKbitsSec = ($deltaInOctets * 8.0) / $deltaTime / 1000.0; $outKbitsSec = ($deltaOutOctets * 8.0) / $deltaTime / 1000.0; } else { log_message('error', "์‹œ๊ฐ„ ์ฐจ์ด ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {$trafficEntity->getIP()} - {$deltaTime}์ดˆ (UID: {$trafficEntity->getPK()})"); } } // DB์— ์ €์žฅํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ return [ // 'raw_in'๊ณผ 'raw_out'์€ ์ด์ œ 64๋น„ํŠธ ์ •์ˆ˜(BIGINT)๋ฅผ ๋‹ด์Šต๋‹ˆ๋‹ค. // PHP์—์„œ (int) ์บ์ŠคํŒ…์€ 64๋น„ํŠธ ํ™˜๊ฒฝ์—์„œ 64๋น„ํŠธ ์ •์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. 'trafficinfo_uid' => (int)$trafficEntity->getPK(), 'in' => (int)$inKbitsSec, // ์ •์ˆ˜ํ˜•์œผ๋กœ ๋ฐ˜ํ™˜ 'out' => (int)$outKbitsSec, // ์ •์ˆ˜ํ˜•์œผ๋กœ ๋ฐ˜ํ™˜ 'raw_in' => (int)$currentInOctets, 'raw_out' => (int)$currentOutOctets, ]; } }