001/* 002 * Stallion Core: A Modern Web Framework 003 * 004 * Copyright (C) 2015 - 2016 Stallion Software LLC. 005 * 006 * This program is free software: you can redistribute it and/or modify it under the terms of the 007 * GNU General Public License as published by the Free Software Foundation, either version 2 of 008 * the License, or (at your option) any later version. This program is distributed in the hope that 009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 011 * License for more details. You should have received a copy of the GNU General Public License 012 * along with this program. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>. 013 * 014 * 015 * 016 */ 017 018package io.stallion.monitoring; 019 020import com.sun.management.UnixOperatingSystemMXBean; 021import io.stallion.services.Log; 022import io.stallion.settings.Settings; 023 024import java.io.File; 025import java.lang.management.ManagementFactory; 026import java.lang.management.OperatingSystemMXBean; 027import java.time.ZonedDateTime; 028 029 030public class SystemHealth { 031 private long jvmMemoryUsage = 0; 032 private long jvmMemoryUsageMb = 0; 033 private long diskFreeDataDirectory; 034 private long diskFreeDataDirectoryMb; 035 private long diskFreeAppDirectory; 036 private long diskFreeLogDirectory; 037 private long fileHandlesOpen = 0; 038 private long fileHandlesMax = 4000; 039 private long fileHandlesAvailable = 4000; 040 private Double memoryPercentFree = 0.0; 041 private long memorySwapSize = 0L; 042 private long memorySwapFree = 0; 043 private long memoryPhysicalSize = 0; 044 private long memoryPhysicalFree = 0; 045 private Double swapPagingRate = 0.0; 046 private Double cpuAppUsage = 0.0; 047 private Double cpuSystemUsage = 0.0; 048 private Double cpuRollingAppUsage = 0.0; 049 private Double cpuRollingSystemUsage = 0.0; 050 private long cpusAvailable = 0; 051 private boolean sslExpiresWithin21Days = false; 052 private boolean sslExpiresWithin7Days = false; 053 private ZonedDateTime sslExpiresDate = null; 054 055 056 057 public SystemHealth hydrateSystemHealth() { 058 Runtime rt = Runtime.getRuntime(); 059 rt.gc(); 060 long memUsage = (rt.totalMemory() - rt.freeMemory()); 061 long usedMB = memUsage / 1024 / 1024; 062 setJvmMemoryUsage(memUsage); 063 setJvmMemoryUsageMb(usedMB); 064 setDiskFreeAppDirectory(new File(Settings.instance().getTargetFolder()).getUsableSpace()); 065 setDiskFreeDataDirectory(new File(Settings.instance().getDataDirectory()).getUsableSpace()); 066 setDiskFreeDataDirectoryMb(new File(Settings.instance().getDataDirectory()).getUsableSpace() / 1024 / 1024); 067 File logFile = new File(Settings.instance().getLogFile()); 068 if (logFile.getParentFile().exists()) { 069 setDiskFreeLogDirectory(logFile.getParentFile().getUsableSpace()); 070 } 071 OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean(); 072 if(os instanceof UnixOperatingSystemMXBean){ 073 UnixOperatingSystemMXBean unixBean = (UnixOperatingSystemMXBean) os; 074 // get system load 075 // get process CPU load 076 // Get free memory 077 memorySwapSize = unixBean.getTotalSwapSpaceSize(); 078 memorySwapFree = unixBean.getFreeSwapSpaceSize(); 079 memoryPhysicalSize = unixBean.getTotalPhysicalMemorySize(); 080 setMemoryPhysicalFree(unixBean.getFreePhysicalMemorySize()); 081 long totalRamFree = unixBean.getFreeSwapSpaceSize() + unixBean.getFreePhysicalMemorySize(); 082 long totalRam = unixBean.getTotalPhysicalMemorySize() + unixBean.getTotalSwapSpaceSize(); 083 setMemoryPercentFree(new Double(totalRamFree) / new Double(totalRam)); 084 setFileHandlesOpen(unixBean.getOpenFileDescriptorCount()); 085 setFileHandlesMax(unixBean.getMaxFileDescriptorCount()); 086 setFileHandlesAvailable(unixBean.getMaxFileDescriptorCount() - unixBean.getOpenFileDescriptorCount()); 087 setCpusAvailable(unixBean.getAvailableProcessors()); 088 089 setCpuAppUsage(unixBean.getProcessCpuLoad()); 090 setCpuSystemUsage(unixBean.getSystemLoadAverage()); 091 } 092 setCpuRollingAppUsage(HealthTracker.instance().getAverageAppCpuLoad()); 093 setCpuRollingSystemUsage(HealthTracker.instance().getAverageSystemCpuLoad()); 094 setSwapPagingRate(HealthTracker.instance().getSwapPages()); 095 setSslExpiresDate(HealthTracker.instance().getSslExpires()); 096 setSslExpiresWithin21Days(HealthTracker.instance().getSslExpiresIn21()); 097 setSslExpiresWithin7Days(HealthTracker.instance().getSslExpiresIn7()); 098 return this; 099 } 100 101 public long getJvmMemoryUsage() { 102 return jvmMemoryUsage; 103 } 104 105 public void setJvmMemoryUsage(long jvmMemoryUsage) { 106 this.jvmMemoryUsage = jvmMemoryUsage; 107 } 108 109 public long getDiskFreeDataDirectory() { 110 return diskFreeDataDirectory; 111 } 112 113 public void setDiskFreeDataDirectory(long diskFreeDataDirectory) { 114 this.diskFreeDataDirectory = diskFreeDataDirectory; 115 } 116 117 public long getDiskFreeAppDirectory() { 118 return diskFreeAppDirectory; 119 } 120 121 public void setDiskFreeAppDirectory(long diskFreeAppDirectory) { 122 this.diskFreeAppDirectory = diskFreeAppDirectory; 123 } 124 125 public long getDiskFreeLogDirectory() { 126 return diskFreeLogDirectory; 127 } 128 129 public void setDiskFreeLogDirectory(long diskFreeLogDirectory) { 130 this.diskFreeLogDirectory = diskFreeLogDirectory; 131 } 132 133 public long getJvmMemoryUsageMb() { 134 return jvmMemoryUsageMb; 135 } 136 137 public void setJvmMemoryUsageMb(long jvmMemoryUsageMb) { 138 this.jvmMemoryUsageMb = jvmMemoryUsageMb; 139 } 140 141 public long getDiskFreeDataDirectoryMb() { 142 return diskFreeDataDirectoryMb; 143 } 144 145 public void setDiskFreeDataDirectoryMb(long diskFreeDataDirectoryMb) { 146 this.diskFreeDataDirectoryMb = diskFreeDataDirectoryMb; 147 } 148 149 public long getFileHandlesOpen() { 150 return fileHandlesOpen; 151 } 152 153 public void setFileHandlesOpen(long fileHandlesOpen) { 154 this.fileHandlesOpen = fileHandlesOpen; 155 } 156 157 public Double getMemoryPercentFree() { 158 return memoryPercentFree; 159 } 160 161 public void setMemoryPercentFree(Double memoryPercentFree) { 162 this.memoryPercentFree = memoryPercentFree; 163 } 164 165 public Double getSwapPagingRate() { 166 return swapPagingRate; 167 } 168 169 public void setSwapPagingRate(Double swapPagingRate) { 170 this.swapPagingRate = swapPagingRate; 171 } 172 173 public Double getCpuAppUsage() { 174 return cpuAppUsage; 175 } 176 177 public void setCpuAppUsage(Double cpuAppUsage) { 178 this.cpuAppUsage = cpuAppUsage; 179 } 180 181 public Double getCpuSystemUsage() { 182 return cpuSystemUsage; 183 } 184 185 public void setCpuSystemUsage(Double cpuSystemUsage) { 186 this.cpuSystemUsage = cpuSystemUsage; 187 } 188 189 public long getFileHandlesMax() { 190 return fileHandlesMax; 191 } 192 193 public void setFileHandlesMax(long fileHandlesMax) { 194 this.fileHandlesMax = fileHandlesMax; 195 } 196 197 public long getFileHandlesAvailable() { 198 return fileHandlesAvailable; 199 } 200 201 public void setFileHandlesAvailable(long fileHandlesAvailable) { 202 this.fileHandlesAvailable = fileHandlesAvailable; 203 } 204 205 public long getCpusAvailable() { 206 return cpusAvailable; 207 } 208 209 public void setCpusAvailable(long cpusAvailable) { 210 this.cpusAvailable = cpusAvailable; 211 } 212 213 public Double getCpuRollingAppUsage() { 214 return cpuRollingAppUsage; 215 } 216 217 public void setCpuRollingAppUsage(Double cpuRollingAppUsage) { 218 this.cpuRollingAppUsage = cpuRollingAppUsage; 219 } 220 221 public Double getCpuRollingSystemUsage() { 222 return cpuRollingSystemUsage; 223 } 224 225 public void setCpuRollingSystemUsage(Double cpuRollingSystemUsage) { 226 this.cpuRollingSystemUsage = cpuRollingSystemUsage; 227 } 228 229 public long getMemorySwapSize() { 230 return memorySwapSize; 231 } 232 233 public void setMemorySwapSize(long memorySwapSize) { 234 this.memorySwapSize = memorySwapSize; 235 } 236 237 public long getMemorySwapFree() { 238 return memorySwapFree; 239 } 240 241 public void setMemorySwapFree(long memorySwapFree) { 242 this.memorySwapFree = memorySwapFree; 243 } 244 245 public long getMemoryPhysicalSize() { 246 return memoryPhysicalSize; 247 } 248 249 public void setMemoryPhysicalSize(long memoryPhysicalSize) { 250 this.memoryPhysicalSize = memoryPhysicalSize; 251 } 252 253 public long getMemoryPhysicalFree() { 254 return memoryPhysicalFree; 255 } 256 257 public void setMemoryPhysicalFree(long memoryPhysicalFree) { 258 this.memoryPhysicalFree = memoryPhysicalFree; 259 } 260 261 public boolean isSslExpiresWithin21Days() { 262 return sslExpiresWithin21Days; 263 } 264 265 public SystemHealth setSslExpiresWithin21Days(boolean sslExpiresWithin21Days) { 266 this.sslExpiresWithin21Days = sslExpiresWithin21Days; 267 return this; 268 } 269 270 public boolean isSslExpiresWithin7Days() { 271 return sslExpiresWithin7Days; 272 } 273 274 public SystemHealth setSslExpiresWithin7Days(boolean sslExpiresWithin7Days) { 275 this.sslExpiresWithin7Days = sslExpiresWithin7Days; 276 return this; 277 } 278 279 public ZonedDateTime getSslExpiresDate() { 280 return sslExpiresDate; 281 } 282 283 public void setSslExpiresDate(ZonedDateTime sslExpiresDate) { 284 this.sslExpiresDate = sslExpiresDate; 285 } 286 287}