From cd63515b0afa603e317cc63e43a3341783ba3e15 Mon Sep 17 00:00:00 2001 From: Ivan Topolnjak Date: Mon, 1 Dec 2014 21:17:26 +0100 Subject: ! sigar,sysmetrics: delete the kamon-sigar project and switch to sigar-loader --- .../scala/kamon/system/SystemMetricsBanner.scala | 91 ++++++++++++++++++ .../kamon/system/SystemMetricsCollector.scala | 38 +++++--- .../scala/kamon/system/sigar/SigarLoader.scala | 102 --------------------- 3 files changed, 115 insertions(+), 116 deletions(-) create mode 100644 kamon-system-metrics/src/main/scala/kamon/system/SystemMetricsBanner.scala delete mode 100644 kamon-system-metrics/src/main/scala/kamon/system/sigar/SigarLoader.scala (limited to 'kamon-system-metrics') diff --git a/kamon-system-metrics/src/main/scala/kamon/system/SystemMetricsBanner.scala b/kamon-system-metrics/src/main/scala/kamon/system/SystemMetricsBanner.scala new file mode 100644 index 00000000..99e09da9 --- /dev/null +++ b/kamon-system-metrics/src/main/scala/kamon/system/SystemMetricsBanner.scala @@ -0,0 +1,91 @@ +/* + * ========================================================================================= + * Copyright © 2013-2014 the kamon project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions + * and limitations under the License. + * ========================================================================================= + */ + +package kamon.system + +import akka.actor.ActorLogging +import org.hyperic.sigar._ + +import scala.util.control.NoStackTrace + +trait SystemMetricsBanner { + self: ActorLogging ⇒ + + def printBanner(sigar: Sigar) = { + val os = OperatingSystem.getInstance + + def loadAverage(sigar: Sigar) = try { + val average = sigar.getLoadAverage + (average(0), average(1), average(2)) + } catch { + case s: org.hyperic.sigar.SigarNotImplementedException ⇒ (0d, 0d, 0d) + } + + def uptime(sigar: Sigar) = { + def formatUptime(uptime: Double): String = { + var retval: String = "" + val days: Int = uptime.toInt / (60 * 60 * 24) + var minutes: Int = 0 + var hours: Int = 0 + + if (days != 0) { + retval += s"$days ${if (days > 1) "days" else "day"}, " + } + + minutes = uptime.toInt / 60 + hours = minutes / 60 + hours %= 24 + minutes %= 60 + + if (hours != 0) { + retval += hours + ":" + minutes + } else { + retval += minutes + " min" + } + retval + } + + val uptime = sigar.getUptime + val now = System.currentTimeMillis() + + s"up ${formatUptime(uptime.getUptime)}" + } + + val message = + """ + | + | _____ _ __ __ _ _ _ _ _ + | / ____| | | | \/ | | | (_) | | | | | | + || (___ _ _ ___| |_ ___ _ __ ___ | \ / | ___| |_ _ __ _ ___ ___| | ___ __ _ __| | ___ __| | + | \___ \| | | / __| __/ _ \ '_ ` _ \| |\/| |/ _ \ __| '__| |/ __/ __| | / _ \ / _` |/ _` |/ _ \/ _` | + | ____) | |_| \__ \ || __/ | | | | | | | | __/ |_| | | | (__\__ \ |___| (_) | (_| | (_| | __/ (_| | + ||_____/ \__, |___/\__\___|_| |_| |_|_| |_|\___|\__|_| |_|\___|___/______\___/ \__,_|\__,_|\___|\__,_| + | __/ | + | |___/ + | + | [System Status] [OS Information] + | |--------------------------------| |----------------------------------------| + | Up Time: %-10s Description: %s + | Load Average: %-16s Name: %s + | Version: %s + | Arch: %s + | + """.stripMargin.format(uptime(sigar), os.getDescription, loadAverage(sigar), os.getName, os.getVersion, os.getArch) + log.info(message) + } + + class UnexpectedSigarException(message: String) extends RuntimeException(message) with NoStackTrace +} diff --git a/kamon-system-metrics/src/main/scala/kamon/system/SystemMetricsCollector.scala b/kamon-system-metrics/src/main/scala/kamon/system/SystemMetricsCollector.scala index b1ebf402..bb7fa105 100644 --- a/kamon-system-metrics/src/main/scala/kamon/system/SystemMetricsCollector.scala +++ b/kamon-system-metrics/src/main/scala/kamon/system/SystemMetricsCollector.scala @@ -15,7 +15,7 @@ */ package kamon.system -import java.io.IOException +import java.io.{ File, IOException } import akka.actor.{ Actor, ActorLogging, Props } import kamon.Kamon @@ -26,19 +26,24 @@ import kamon.metrics.MemoryMetrics.MemoryMetricRecorder import kamon.metrics.NetworkMetrics.NetworkMetricRecorder import kamon.metrics.ProcessCPUMetrics.ProcessCPUMetricsRecorder import kamon.metrics._ -import kamon.system.sigar.SigarLoader -import org.hyperic.sigar.{ Mem, NetInterfaceStat, SigarProxy } +import kamon.sigar.SigarProvisioner +import org.hyperic.sigar.{ Sigar, Mem, NetInterfaceStat, SigarProxy } import scala.concurrent.duration.FiniteDuration import scala.io.Source +import scala.util.control.NonFatal -class SystemMetricsCollector(collectInterval: FiniteDuration) extends Actor with ActorLogging with SigarExtensionProvider { +class SystemMetricsCollector(collectInterval: FiniteDuration) extends Actor with ActorLogging with SystemMetricsBanner { import kamon.system.SystemMetricsCollector._ import kamon.system.SystemMetricsExtension._ - val collectSchedule = context.system.scheduler.schedule(collectInterval, collectInterval, self, Collect)(context.dispatcher) + lazy val sigar = createSigarInstance + def pid = sigar.getPid + + val interfaces: Set[String] = sigar.getNetInterfaceList.toSet val systemMetricsExtension = Kamon(Metrics)(context.system) + val collectSchedule = context.system.scheduler.schedule(collectInterval, collectInterval, self, Collect)(context.dispatcher) val cpuRecorder = systemMetricsExtension.register(CPUMetrics(CPU), CPUMetrics.Factory) val processCpuRecorder = systemMetricsExtension.register(ProcessCPUMetrics(ProcessCPU), ProcessCPUMetrics.Factory) @@ -152,6 +157,19 @@ class SystemMetricsCollector(collectInterval: FiniteDuration) extends Actor with rcs.perProcessNonVoluntary.record(perProcessNonVoluntary) rcs.global.record(contextSwitches) } + + def createSigarInstance: SigarProxy = { + val tempFolder = new File(System.getProperty("java.io.tmpdir")) + + try { + SigarProvisioner.provision(tempFolder) + val sigar = new Sigar() + printBanner(sigar) + sigar + } catch { + case NonFatal(t) ⇒ throw new UnexpectedSigarException("Failed to load sigar") + } + } } object SystemMetricsCollector { @@ -162,12 +180,4 @@ object SystemMetricsCollector { } def props(collectInterval: FiniteDuration): Props = Props[SystemMetricsCollector](new SystemMetricsCollector(collectInterval)) -} - -trait SigarExtensionProvider { - lazy val sigar = SigarLoader.instance - - def pid = sigar.getPid - - val interfaces: Set[String] = sigar.getNetInterfaceList.toSet -} +} \ No newline at end of file diff --git a/kamon-system-metrics/src/main/scala/kamon/system/sigar/SigarLoader.scala b/kamon-system-metrics/src/main/scala/kamon/system/sigar/SigarLoader.scala deleted file mode 100644 index 1b33fd6f..00000000 --- a/kamon-system-metrics/src/main/scala/kamon/system/sigar/SigarLoader.scala +++ /dev/null @@ -1,102 +0,0 @@ -/* - * ========================================================================================= - * Copyright © 2013-2014 the kamon project - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the - * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, - * either express or implied. See the License for the specific language governing permissions - * and limitations under the License. - * ========================================================================================= - */ - -package kamon.system.sigar - -import java.io._ -import java.util.logging.Logger -import kamon.sigar.SigarAgent -import org.hyperic.sigar._ -import scala.util.control.{ NoStackTrace, NonFatal } - -object SigarLoader { - private val log = Logger.getLogger("SigarLoader") - - lazy val instance = init(new File(System.getProperty("java.io.tmpdir"))) - - private[sigar] def init(baseTmp: File): SigarProxy = try { - SigarAgent.provision(baseTmp) - val sigar = new Sigar() - printBanner(sigar) - sigar - } catch { - case NonFatal(t) ⇒ throw new UnexpectedSigarException("Failed to load sigar") - } - - private[sigar] def printBanner(sigar: Sigar) = { - val os = OperatingSystem.getInstance - - def loadAverage(sigar: Sigar) = try { - val average = sigar.getLoadAverage - (average(0), average(1), average(2)) - } catch { - case s: org.hyperic.sigar.SigarNotImplementedException ⇒ (0d, 0d, 0d) - } - - def uptime(sigar: Sigar) = { - def formatUptime(uptime: Double): String = { - var retval: String = "" - val days: Int = uptime.toInt / (60 * 60 * 24) - var minutes: Int = 0 - var hours: Int = 0 - - if (days != 0) { - retval += s"$days ${if (days > 1) "days" else "day"}, " - } - - minutes = uptime.toInt / 60 - hours = minutes / 60 - hours %= 24 - minutes %= 60 - - if (hours != 0) { - retval += hours + ":" + minutes - } else { - retval += minutes + " min" - } - retval - } - - val uptime = sigar.getUptime - val now = System.currentTimeMillis() - - s"up ${formatUptime(uptime.getUptime)}" - } - - val message = - """ - | - | _____ _ __ __ _ _ _ _ _ - | / ____| | | | \/ | | | (_) | | | | | | - || (___ _ _ ___| |_ ___ _ __ ___ | \ / | ___| |_ _ __ _ ___ ___| | ___ __ _ __| | ___ __| | - | \___ \| | | / __| __/ _ \ '_ ` _ \| |\/| |/ _ \ __| '__| |/ __/ __| | / _ \ / _` |/ _` |/ _ \/ _` | - | ____) | |_| \__ \ || __/ | | | | | | | | __/ |_| | | | (__\__ \ |___| (_) | (_| | (_| | __/ (_| | - ||_____/ \__, |___/\__\___|_| |_| |_|_| |_|\___|\__|_| |_|\___|___/______\___/ \__,_|\__,_|\___|\__,_| - | __/ | - | |___/ - | - | [System Status] [OS Information] - | |--------------------------------| |----------------------------------------| - | Up Time: %-10s Description: %s - | Load Average: %-16s Name: %s - | Version: %s - | Arch: %s - | - """.stripMargin.format(uptime(sigar), os.getDescription, loadAverage(sigar), os.getName, os.getVersion, os.getArch) - log.info(message) - } - class UnexpectedSigarException(message: String) extends RuntimeException(message) with NoStackTrace -} -- cgit v1.2.3