daemon-idc/app/Libraries/SNMPLibrary.php
2026-02-09 16:48:11 +09:00

38 lines
1.3 KiB
PHP

<?php
namespace App\Libraries;
class SNMPLibrary
{
private $_ip = null;
private $_community = null;
private $_version = '2c';
public function __construct(string $ip, string $community, ?string $version = null)
{
$this->_ip = $ip;
$this->_community = $community;
}
public function get(string $fullOid): ?int
{
// snmp2_get을 사용하여 SNMP v2c로 요청
// 💡 snmp2_get() 함수가 존재하지 않는다는 LSP 오류를 무시하기 위해 @suppress 태그 사용
/** @phpstan-ignore-next-line */
$result = @snmp2_get($this->_ip, $this->_community, $fullOid, 100000, 3);
if ($result === false || $result === null) {
log_message('error', "SNMP 통신 실패: {$this->_ip} ({$fullOid}, Community: {$this->_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;
}
}