@@ -312,6 +312,15 @@ public String toString() {
312312
313313 protected static ConfigKey <Integer > vmDiskStatsMaxRetentionTime = new ConfigKey <>("Advanced" , Integer .class , "vm.disk.stats.max.retention.time" , "720" ,
314314 "The maximum time (in minutes) for keeping VM disks stats records in the database. The VM disks stats cleanup process will be disabled if this is set to 0 or less than 0." , true );
315+ protected static final ConfigKey <Boolean > MANAGEMENT_DB_AUTODELETE_ENABLED_BY_THRESHOLD =
316+ new ConfigKey <>(
317+ "Advanced" ,
318+ Boolean .class ,
319+ "management.db.autodelete.enabled.by.threshold" ,
320+ "false" ,
321+ "Enable automatic deletion of oldest records in the 'event' table when DB filesystem usage exceeds threshold." ,
322+ true
323+ );
315324
316325 private static StatsCollector s_instance = null ;
317326
@@ -1085,13 +1094,56 @@ private void checkMngtServerStorageCapacityThreshold(ManagementServerHostStatsEn
10851094 }
10861095
10871096 private void checkDBCapacityThreshold (@ NotNull ManagementServerHostStatsEntry newEntry ) {
1088- // Check mysql server storage capacity exceeds a set threshold
1089- String managementServerDatabaseStorageCapacityThreshold = (_configDao .getValue (Config .ManagementServerDatabaseStorageCapacityThreshold .key ()).replace ("." , "" ));
1097+ // ON/OFF 설정 읽기
1098+ final boolean isAutoDeleteEnabled = Boolean .TRUE .equals (MANAGEMENT_DB_AUTODELETE_ENABLED_BY_THRESHOLD .value ());
1099+ // 비활성화면 즉시 종료 (플래그 해제 + 로그)
1100+ if (!isAutoDeleteEnabled ) {
1101+ DELETE_EVENT_ACTIVE = false ;
1102+ if (logger .isDebugEnabled ()) {
1103+ logger .debug ("Auto-delete disabled (management.db.autodelete.enabled.by.threshold=false). Skipping delete." );
1104+ }
1105+ return ;
1106+ }
1107+ // DB 삭제 임계치 추출
1108+ String rawThreshold = _configDao .getValue (Config .ManagementServerDatabaseStorageCapacityThreshold .key ()).trim ();
1109+ double th = Double .parseDouble (rawThreshold );
1110+ String managementServerDatabaseStorageCapacityThreshold = Integer .toString (
1111+ (int ) Math .round (th <= 1.0 ? th * 100.0 : th ));
10901112 int intMysqlThreshold = Integer .parseInt (managementServerDatabaseStorageCapacityThreshold );
1091- // Percent free of the Filesystem mounted with that path(var: mysqlDataDir).
1092- String mysqlDataDir = "/var/lib/mysql/" ;
1093- String mysqlDuThreshold = (Script .runSimpleBashScript ("df -h " +mysqlDataDir +" | awk 'NR==2 {print $5}'" ).replace ("%" , "" ));
1094- int intDfThreshold = Integer .parseInt (mysqlDuThreshold );
1113+ // mysql 경로 추출
1114+ String mysqlDataDir = null ;
1115+ TransactionLegacy txn0 = TransactionLegacy .open ("getDatadir" );
1116+ try {
1117+ txn0 .start ();
1118+ Connection conn = txn0 .getConnection ();
1119+ try (PreparedStatement ps = conn .prepareStatement ("SELECT @@datadir" );
1120+ ResultSet rs = ps .executeQuery ()) {
1121+ if (rs .next ()) {
1122+ mysqlDataDir = rs .getString (1 );
1123+ }
1124+ }
1125+ txn0 .commit ();
1126+ } catch (Exception e ) {
1127+ logger .warn ("Failed to read @@datadir, fallback to /var/lib/mysql/: " + e .getMessage ());
1128+ } finally {
1129+ try { if (txn0 != null ) { txn0 .close (); } } catch (Exception ignore ) { }
1130+ }
1131+ if (mysqlDataDir == null || mysqlDataDir .isEmpty ()) {
1132+ mysqlDataDir = "/var/lib/mysql/" ;
1133+ }
1134+ // mysql 경로 파티션 용량 추출
1135+ String cmd = "LC_ALL=C df -P \" " + mysqlDataDir + "\" | awk 'NR==2 {gsub(/%/, \" \" ); print $5}'" ;
1136+ String mysqlDuThreshold = Script .runSimpleBashScript (cmd );
1137+ mysqlDuThreshold = mysqlDuThreshold == null ? "" : mysqlDuThreshold .trim ();
1138+ int intDfThreshold ;
1139+ try {
1140+ intDfThreshold = Integer .parseInt (mysqlDuThreshold );
1141+ } catch (NumberFormatException e ) {
1142+ logger .warn ("Cannot parse df percent for " + mysqlDataDir + ": '" + mysqlDuThreshold + "'. Skip this cycle." , e );
1143+ return ;
1144+ }
1145+ intDfThreshold = Math .max (0 , Math .min (100 , intDfThreshold ));
1146+ DELETE_EVENT_ACTIVE = false ;
10951147 if (intDfThreshold > intMysqlThreshold ) {
10961148 DELETE_EVENT_ACTIVE = true ;
10971149 // Every 720 minutes(= 12 hours)
@@ -2415,7 +2467,8 @@ public ConfigKey<?>[] getConfigKeys() {
24152467 vmStatsIncrementMetrics , vmStatsMaxRetentionTime , vmStatsCollectUserVMOnly , vmDiskStatsRetentionEnabled , vmDiskStatsMaxRetentionTime ,
24162468 MANAGEMENT_SERVER_STATUS_COLLECTION_INTERVAL ,
24172469 DATABASE_SERVER_STATUS_COLLECTION_INTERVAL ,
2418- DATABASE_SERVER_LOAD_HISTORY_RETENTION_NUMBER };
2470+ DATABASE_SERVER_LOAD_HISTORY_RETENTION_NUMBER ,
2471+ MANAGEMENT_DB_AUTODELETE_ENABLED_BY_THRESHOLD };
24192472 }
24202473
24212474 public double getImageStoreCapacityThreshold () {
0 commit comments