40 |
41 |
{{measurements[0].name}} - {{measurements[0].url}}
42 |
43 |
44 |
49 |
50 |
51 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/src/main/java/ch/simas/monitor/boundry/CheckController.kt:
--------------------------------------------------------------------------------
1 | package ch.simas.monitor.boundry
2 |
3 | import ch.simas.monitor.control.MeasurementRepository
4 | import ch.simas.monitor.entity.Measurement
5 | import ch.simas.monitor.xml.Hosts
6 | import org.apache.commons.logging.Log
7 | import org.apache.commons.logging.LogFactory
8 | import java.io.File
9 | import java.io.IOException
10 | import java.net.HttpURLConnection
11 | import java.net.URL
12 | import java.time.format.DateTimeFormatter
13 | import javax.xml.bind.JAXBContext
14 | import javax.xml.bind.JAXBException
15 | import javax.xml.bind.Unmarshaller
16 | import org.springframework.beans.factory.annotation.Autowired
17 | import org.springframework.beans.factory.annotation.Value
18 | import org.springframework.scheduling.annotation.Scheduled
19 | import org.springframework.web.bind.annotation.RequestMapping
20 | import org.springframework.web.bind.annotation.RestController
21 |
22 | @RestController
23 | @RequestMapping("/check")
24 | open class CheckController(@Value("\${simon.config.hosts}") val config: String, @Autowired val measurementRepository: MeasurementRepository) {
25 |
26 | private val log: Log = LogFactory.getLog(CheckController::class.toString())
27 |
28 | @RequestMapping("/latest")
29 | fun getLatest(): Hosts {
30 | val hosts = loadConfiguration(config)
31 |
32 | hosts.group.forEach { group ->
33 | group.host.forEach { host ->
34 | val measurements = measurementRepository.find(host.url, 1, null, null)
35 | if (!measurements.isEmpty()) {
36 | val measurement = measurements[0]
37 | host.status = measurement.status
38 | host.duration = measurement.duration
39 | host.timestamp = measurement.timestamp.format(DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss"))
40 | }
41 | }
42 | }
43 | return hosts
44 | }
45 |
46 | @RequestMapping
47 | @Scheduled(initialDelay = 1 * 60 * 1000, fixedRate = 1 * 60 * 5000)
48 | fun check() {
49 | try {
50 | val hosts = loadConfiguration(config)
51 |
52 | hosts.group.forEach { group ->
53 | group.host.forEach { host ->
54 | val start = System.currentTimeMillis()
55 | val obj = URL(host.url)
56 | try {
57 | val connection = obj.openConnection() as HttpURLConnection
58 | val responseCode = connection.responseCode
59 | host.status = "" + responseCode
60 | host.duration = System.currentTimeMillis() - start
61 | connection.disconnect();
62 | } catch (e: Exception) {
63 | log.error(e.message, e)
64 | host.status = e.message
65 | }
66 | measurementRepository.createMeasurement(host.name, host.url, host.status, host.duration)
67 | }
68 | }
69 | } catch (e: IOException) {
70 | throw RuntimeException(e)
71 | }
72 | }
73 |
74 | private fun loadConfiguration(configFile: String): Hosts {
75 | try {
76 | val file = File(configFile)
77 | val jaxbContext = JAXBContext.newInstance(Hosts::class.java)
78 |
79 | val unmarshaller = jaxbContext.createUnmarshaller()
80 | val hosts = unmarshaller.unmarshal(file) as Hosts
81 | return hosts
82 | } catch (e: JAXBException) {
83 | throw RuntimeException(e)
84 | }
85 |
86 | }
87 |
88 | }
89 |
--------------------------------------------------------------------------------
/src/main/resources/static/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
68 |
69 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |