-
Notifications
You must be signed in to change notification settings - Fork 640
/
Copy pathMonitoringCtrl.scala
47 lines (40 loc) · 1.52 KB
/
MonitoringCtrl.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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 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")
.authPermittedTransaction(db, Permissions.managePlatform)(implicit request =>
implicit graph =>
for {
_ <- Success(())
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))
)
}