diff --git a/app/Services/CollectorService.php b/app/Services/CollectorService.php index 3116089..8d6bb10 100644 --- a/app/Services/CollectorService.php +++ b/app/Services/CollectorService.php @@ -15,44 +15,52 @@ class CollectorService extends CommonService { private $_form = null; private $_helper = null; - const OID_IF_IN_OCTETS = '1.3.6.1.2.1.2.2.1.10.'; // ifInOctets (Raw Octets) - const OID_IF_OUT_OCTETS = '1.3.6.1.2.1.2.2.1.16.'; // ifOutOctets (Raw Octets) + // ๐Ÿ’ก 64๋น„ํŠธ ์นด์šดํ„ฐ๋ฅผ ์œ„ํ•ด High Capacity OID๋กœ ๋ณ€๊ฒฝ: ifHCInOctets + const OID_IF_IN_OCTETS = '1.3.6.1.2.1.31.1.1.1.6.'; + // ๐Ÿ’ก 64๋น„ํŠธ ์นด์šดํ„ฐ๋ฅผ ์œ„ํ•ด High Capacity OID๋กœ ๋ณ€๊ฒฝ: ifHCOutOctets + const OID_IF_OUT_OCTETS = '1.3.6.1.2.1.31.1.1.1.10.'; + const SNMP_VERSION = '2c'; - // Counter32์˜ ์ตœ๋Œ€๊ฐ’ + 1 (์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ๋ณด์ •์„ ์œ„ํ•ด ์‚ฌ์šฉ) - const MAX_COUNTER_32BIT = 4294967296; + + // ๐Ÿ’ก 32๋น„ํŠธ ๋กค์˜ค๋ฒ„ ๋กœ์ง์„ ์ œ๊ฑฐํ–ˆ์œผ๋ฏ€๋กœ ํ•ด๋‹น ์ƒ์ˆ˜๋Š” ํ•„์š” ์—†์Šต๋‹ˆ๋‹ค. + // const MAX_COUNTER_32BIT = 4294967296; + public function __construct(CollectorModel $model) { parent::__construct($model); $this->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(), + 'pk_field' => $this->model->getPKField(), + 'title_field' => $this->model->getTitleField(), 'table' => $this->model->getTable(), 'useAutoIncrement' => $this->model->useAutoIncrement(), - 'class_path' => $this->getClassPaths(false), + '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(), + 'pk_field' => $this->model->getPKField(), + 'title_field' => $this->model->getTitleField(), 'table' => $this->model->getTable(), 'useAutoIncrement' => $this->model->useAutoIncrement(), - 'class_path' => $this->getClassPaths(false), + 'class_path' => $this->getClassPaths(false), ]); } return $this->_helper; } + //๊ธฐ๋ณธ ๊ธฐ๋Šฅ๋ถ€๋ถ„ public function create(object $dto): CollectorEntity { @@ -61,6 +69,7 @@ class CollectorService extends CommonService } return parent::create($dto); } + public function modify($uid, object $dto): CollectorEntity { if (!$dto instanceof CollectorDTO) { @@ -68,6 +77,7 @@ class CollectorService extends CommonService } return parent::modify($uid, $dto); } + //List ๊ฒ€์ƒ‰์šฉ //FormFilter ์กฐ๊ฑด์ ˆ ์ฒ˜๋ฆฌ //๊ฒ€์ƒ‰์–ด์กฐ๊ฑด์ ˆ์ฒ˜๋ฆฌ @@ -76,6 +86,7 @@ class CollectorService extends CommonService $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 { @@ -85,28 +96,34 @@ class CollectorService extends CommonService } return $entities; } + //SNMP์—ฐ๊ฒฐ private function getSNMPOctets(TrafficEntity $trafficEntity, string $oid): ?int { - $fullOid = $oid . $trafficEntity->getInterface(); + $fullOid = $oid . $trafficEntity->getInterface(); $community = $trafficEntity->getCommunity(); - $ip = $trafficEntity->getIP(); + $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๋Š” BigInt๊ฐ€ ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ intval ๋Œ€์‹  (int) ์บ์ŠคํŒ…์„ ์‚ฌ์šฉํ•˜์—ฌ - // ์ตœ๋Œ€ํ•œ ํฐ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. PHP 64๋น„ํŠธ ํ™˜๊ฒฝ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. + // 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); @@ -131,28 +148,19 @@ class CollectorService extends CommonService $deltaTime = Time::now()->getTimestamp() - $lastTime; if ($deltaTime > 0) { + // DB์—์„œ ๊ฐ€์ ธ์˜จ ๊ฐ’๋„ BIGINT ํƒ€์ž…์œผ๋กœ ์ž˜ ์ฒ˜๋ฆฌ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. + // PHP 64๋น„ํŠธ ํ™˜๊ฒฝ์ด๋ผ๋ฉด ์ž๋™์œผ๋กœ 64๋น„ํŠธ ์ •์ˆ˜๋กœ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. $lastIn = $lastEntity->getRawIn(); $lastOut = $lastEntity->getRawOut(); - // ๐Ÿ’ก 1. ์ธ๋ฐ”์šด๋“œ Octets ์ฐจ๋ถ„ ๊ณ„์‚ฐ (์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ์ฒ˜๋ฆฌ) - if ($currentInOctets < $lastIn) { - // ์นด์šดํ„ฐ ๋กค์˜ค๋ฒ„ ๋ฐœ์ƒ: Delta = (MAX - Last) + Current - $deltaInOctets = (self::MAX_COUNTER_32BIT - $lastIn) + $currentInOctets; - log_message('info', "Inbound Rollover Detected for UID: {$trafficEntity->getPK()}. Delta: {$deltaInOctets}"); - } else { - // ์ •์ƒ์ ์ธ ์ฐจ๋ถ„ - $deltaInOctets = $currentInOctets - $lastIn; - } + // ๐Ÿ’ก 1. ์ธ๋ฐ”์šด๋“œ Octets ์ฐจ๋ถ„ ๊ณ„์‚ฐ (32๋น„ํŠธ ๋กค์˜ค๋ฒ„ ๋กœ์ง ์ œ๊ฑฐ) + // 64๋น„ํŠธ ์นด์šดํ„ฐ(BIGINT)๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์ˆœ ๋บ„์…ˆ์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. + // 64๋น„ํŠธ ์นด์šดํ„ฐ๋Š” ์‹ค์งˆ์ ์œผ๋กœ ๋กค์˜ค๋ฒ„๋  ์ผ์ด ์—†์Šต๋‹ˆ๋‹ค. + $deltaInOctets = $currentInOctets - $lastIn; - // ๐Ÿ’ก 2. ์•„์›ƒ๋ฐ”์šด๋“œ Octets ์ฐจ๋ถ„ ๊ณ„์‚ฐ (์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ์ฒ˜๋ฆฌ) - if ($currentOutOctets < $lastOut) { - // ์นด์šดํ„ฐ ๋กค์˜ค๋ฒ„ ๋ฐœ์ƒ - $deltaOutOctets = (self::MAX_COUNTER_32BIT - $lastOut) + $currentOutOctets; - log_message('info', "Outbound Rollover Detected for UID: {$trafficEntity->getPK()}. Delta: {$deltaOutOctets}"); - } else { - // ์ •์ƒ์ ์ธ ์ฐจ๋ถ„ - $deltaOutOctets = $currentOutOctets - $lastOut; - } + // ๐Ÿ’ก 2. ์•„์›ƒ๋ฐ”์šด๋“œ Octets ์ฐจ๋ถ„ ๊ณ„์‚ฐ (32๋น„ํŠธ ๋กค์˜ค๋ฒ„ ๋กœ์ง ์ œ๊ฑฐ) + // 64๋น„ํŠธ ์นด์šดํ„ฐ(BIGINT)๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์ˆœ ๋บ„์…ˆ์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. + $deltaOutOctets = $currentOutOctets - $lastOut; // Kbit/s ๊ณ„์‚ฐ: (Delta_Octets * 8 bits) / Delta_Time_Seconds / 1000 (-> Kbit/s) // ์‹ค์ˆ˜(float) ์—ฐ์‚ฐ์œผ๋กœ ์ •ํ™•๋„๋ฅผ ๋†’์ž…๋‹ˆ๋‹ค. @@ -165,12 +173,13 @@ class CollectorService extends CommonService // DB์— ์ €์žฅํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ return [ - // ๐Ÿ’ก ์š”์ฒญ์— ๋”ฐ๋ผ 'in'๊ณผ 'out' ๊ฐ’์„ ์ •์ˆ˜ํ˜•์œผ๋กœ ์บ์ŠคํŒ…ํ•˜์—ฌ ์†Œ์ˆ˜์  ์ดํ•˜๋ฅผ ๋ฒ„๋ฆผ + // 'raw_in'๊ณผ 'raw_out'์€ ์ด์ œ 64๋น„ํŠธ ์ •์ˆ˜(BIGINT)๋ฅผ ๋‹ด์Šต๋‹ˆ๋‹ค. + // PHP์—์„œ (int) ์บ์ŠคํŒ…์€ 64๋น„ํŠธ ํ™˜๊ฒฝ์—์„œ 64๋น„ํŠธ ์ •์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. 'trafficinfo_uid' => (int)$trafficEntity->getPK(), - 'in' => (int)$inKbitsSec, // ์ •์ˆ˜ํ˜•์œผ๋กœ ๋ฐ˜ํ™˜ + 'in' => (int)$inKbitsSec, // ์ •์ˆ˜ํ˜•์œผ๋กœ ๋ฐ˜ํ™˜ 'out' => (int)$outKbitsSec, // ์ •์ˆ˜ํ˜•์œผ๋กœ ๋ฐ˜ํ™˜ - 'raw_in' => (int)$currentInOctets, - 'raw_out' => (int)$currentOutOctets, + 'raw_in' => (int)$currentInOctets, + 'raw_out' => (int)$currentOutOctets, ]; } }