From 143f16ac889a4e7056e13da572859955dae04285 Mon Sep 17 00:00:00 2001 From: Robin Riclet Date: Wed, 17 Mar 2021 15:38:30 +0100 Subject: [PATCH 1/3] #1843 WIP added disk monitoring route --- ScalliGraph | 2 +- .../controllers/v1/MonitoringCtrl.scala | 48 +++++++++++++++++++ .../thp/thehive/controllers/v1/Router.scala | 3 ++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 thehive/app/org/thp/thehive/controllers/v1/MonitoringCtrl.scala diff --git a/ScalliGraph b/ScalliGraph index 499de18528..0657450b06 160000 --- a/ScalliGraph +++ b/ScalliGraph @@ -1 +1 @@ -Subproject commit 499de18528e24013c5b0280c8d88a870ebfb33b9 +Subproject commit 0657450b06a9b35b1bfa3a7e8ed89c1246140637 diff --git a/thehive/app/org/thp/thehive/controllers/v1/MonitoringCtrl.scala b/thehive/app/org/thp/thehive/controllers/v1/MonitoringCtrl.scala new file mode 100644 index 0000000000..f4e210599c --- /dev/null +++ b/thehive/app/org/thp/thehive/controllers/v1/MonitoringCtrl.scala @@ -0,0 +1,48 @@ +package org.thp.thehive.controllers.v1 + +import org.thp.scalligraph.controllers.Entrypoint +import org.thp.scalligraph.models.Database +import org.thp.scalligraph.services.config.{ApplicationConfig, ConfigItem} +import org.thp.thehive.models.Permissions +import play.api.libs.json.{Format, JsArray, Json} +import play.api.mvc.{Action, AnyContent, Results} + +import java.io.File +import javax.inject.{Inject, Singleton} +import scala.util.Success + +@Singleton +class MonitoringCtrl @Inject() ( + appConfig: ApplicationConfig, + entrypoint: Entrypoint, + db: Database +) { + case class PartitionConfig(location: String) + + object PartitionConfig { + implicit val format: Format[PartitionConfig] = Json.format[PartitionConfig] + } + + val diskLocations: ConfigItem[Seq[PartitionConfig], Seq[PartitionConfig]] = + appConfig.item[Seq[PartitionConfig]]("monitor.disk", "disk locations to monitor") + + def monitorDiskUsage: Action[AnyContent] = + entrypoint("monitor disk usage") + .authPermittedTransaction(db, Permissions.managePlatform)(implicit request => + implicit graph => + for { + _ <- Success(()) + locations = + diskLocations + .get + .foldLeft[JsArray](JsArray.empty)((array, p) => + array :+ Json.obj( + "location" -> p.location, + "freeSpace" -> new File(p.location).getFreeSpace, + "totalSpace" -> new File(p.location).getTotalSpace + ) + ) + } yield Results.Ok(locations) + ) + +} diff --git a/thehive/app/org/thp/thehive/controllers/v1/Router.scala b/thehive/app/org/thp/thehive/controllers/v1/Router.scala index 39e95117d6..7c42c78230 100644 --- a/thehive/app/org/thp/thehive/controllers/v1/Router.scala +++ b/thehive/app/org/thp/thehive/controllers/v1/Router.scala @@ -20,6 +20,7 @@ class Router @Inject() ( // dashboardCtrl: DashboardCtrl, describeCtrl: DescribeCtrl, logCtrl: LogCtrl, + monitoringCtrl: MonitoringCtrl, observableCtrl: ObservableCtrl, observableTypeCtrl: ObservableTypeCtrl, organisationCtrl: OrganisationCtrl, @@ -177,5 +178,7 @@ class Router @Inject() ( case GET(p"/observable/type/$idOrName") => observableTypeCtrl.get(idOrName) case POST(p"/observable/type") => observableTypeCtrl.create case DELETE(p"/observable/type/$idOrName") => observableTypeCtrl.delete(idOrName) + + case GET(p"monitor/disk") => monitoringCtrl.monitorDiskUsage } } From fbc042d2d45841904e06e4a09a3167eb3ceb94b0 Mon Sep 17 00:00:00 2001 From: Robin Riclet Date: Wed, 17 Mar 2021 16:06:28 +0100 Subject: [PATCH 2/3] #1843 fixed disk monitoring route --- ScalliGraph | 2 +- thehive/app/org/thp/thehive/controllers/v1/MonitoringCtrl.scala | 2 +- thehive/app/org/thp/thehive/controllers/v1/Router.scala | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ScalliGraph b/ScalliGraph index 0657450b06..b403c2c2db 160000 --- a/ScalliGraph +++ b/ScalliGraph @@ -1 +1 @@ -Subproject commit 0657450b06a9b35b1bfa3a7e8ed89c1246140637 +Subproject commit b403c2c2db7e163550022283512c1f6d0c9fe91a diff --git a/thehive/app/org/thp/thehive/controllers/v1/MonitoringCtrl.scala b/thehive/app/org/thp/thehive/controllers/v1/MonitoringCtrl.scala index f4e210599c..a6dc213d67 100644 --- a/thehive/app/org/thp/thehive/controllers/v1/MonitoringCtrl.scala +++ b/thehive/app/org/thp/thehive/controllers/v1/MonitoringCtrl.scala @@ -26,7 +26,7 @@ class MonitoringCtrl @Inject() ( val diskLocations: ConfigItem[Seq[PartitionConfig], Seq[PartitionConfig]] = appConfig.item[Seq[PartitionConfig]]("monitor.disk", "disk locations to monitor") - def monitorDiskUsage: Action[AnyContent] = + def diskUsage: Action[AnyContent] = entrypoint("monitor disk usage") .authPermittedTransaction(db, Permissions.managePlatform)(implicit request => implicit graph => diff --git a/thehive/app/org/thp/thehive/controllers/v1/Router.scala b/thehive/app/org/thp/thehive/controllers/v1/Router.scala index 7c42c78230..652fbdd85d 100644 --- a/thehive/app/org/thp/thehive/controllers/v1/Router.scala +++ b/thehive/app/org/thp/thehive/controllers/v1/Router.scala @@ -179,6 +179,6 @@ class Router @Inject() ( case POST(p"/observable/type") => observableTypeCtrl.create case DELETE(p"/observable/type/$idOrName") => observableTypeCtrl.delete(idOrName) - case GET(p"monitor/disk") => monitoringCtrl.monitorDiskUsage + case GET(p"/monitor/disk") => monitoringCtrl.diskUsage } } From 7022b9f5dbc3cf2cb09678c49b987847b2acb8c3 Mon Sep 17 00:00:00 2001 From: To-om Date: Thu, 18 Mar 2021 10:57:01 +0100 Subject: [PATCH 3/3] #1843 Use map instead of foldLeft --- .../controllers/v1/MonitoringCtrl.scala | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/thehive/app/org/thp/thehive/controllers/v1/MonitoringCtrl.scala b/thehive/app/org/thp/thehive/controllers/v1/MonitoringCtrl.scala index a6dc213d67..3729b0a96d 100644 --- a/thehive/app/org/thp/thehive/controllers/v1/MonitoringCtrl.scala +++ b/thehive/app/org/thp/thehive/controllers/v1/MonitoringCtrl.scala @@ -23,8 +23,9 @@ class MonitoringCtrl @Inject() ( implicit val format: Format[PartitionConfig] = Json.format[PartitionConfig] } - val diskLocations: ConfigItem[Seq[PartitionConfig], Seq[PartitionConfig]] = + val diskLocationsConfig: ConfigItem[Seq[PartitionConfig], Seq[PartitionConfig]] = appConfig.item[Seq[PartitionConfig]]("monitor.disk", "disk locations to monitor") + def diskLocations: Seq[PartitionConfig] = diskLocationsConfig.get def diskUsage: Action[AnyContent] = entrypoint("monitor disk usage") @@ -32,17 +33,15 @@ class MonitoringCtrl @Inject() ( implicit graph => for { _ <- Success(()) - locations = - diskLocations - .get - .foldLeft[JsArray](JsArray.empty)((array, p) => - array :+ Json.obj( - "location" -> p.location, - "freeSpace" -> new File(p.location).getFreeSpace, - "totalSpace" -> new File(p.location).getTotalSpace - ) - ) - } yield Results.Ok(locations) + locations = diskLocations.map { dl => + val file = new File(dl.location) + Json.obj( + "location" -> dl.location, + "freeSpace" -> file.getFreeSpace, + "totalSpace" -> file.getTotalSpace + ) + } + } yield Results.Ok(JsArray(locations)) ) }