API: status
This commit is contained in:
parent
9132401ce0
commit
1e1f337ecd
8 changed files with 75 additions and 14 deletions
|
|
@ -2,6 +2,10 @@
|
||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
## [v2.0.4] - 2024-
|
## [v2.0.4] - 2024-
|
||||||
|
### Added
|
||||||
|
- Notification test [#147](https://github.com/aceberg/WatchYourLAN/issues/147)
|
||||||
|
- API status [#148](https://github.com/aceberg/WatchYourLAN/issues/148)
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
- [#101](https://github.com/aceberg/WatchYourLAN/issues/101)
|
- [#101](https://github.com/aceberg/WatchYourLAN/issues/101)
|
||||||
- The same problem for Theme, Color mode, Log level
|
- The same problem for Theme, Color mode, Log level
|
||||||
|
|
|
||||||
12
docs/API.md
12
docs/API.md
|
|
@ -40,3 +40,15 @@ Edit host with ID `id`. Can change `name`. `known` is optional, when set to `tog
|
||||||
GET /api/host/del/:id
|
GET /api/host/del/:id
|
||||||
```
|
```
|
||||||
Remove host with ID `id`.
|
Remove host with ID `id`.
|
||||||
|
|
||||||
|
|
||||||
|
```http
|
||||||
|
GET /api/notify_test
|
||||||
|
```
|
||||||
|
Send test notification.
|
||||||
|
|
||||||
|
|
||||||
|
```http
|
||||||
|
GET /api/status/*iface
|
||||||
|
```
|
||||||
|
Show status (Total number of hosts, online/offline, known/unknown). The `iface` parameter is optional and shows status for one interface only. For all interfaces just call `/api/status/`.
|
||||||
|
|
@ -44,6 +44,15 @@ type Host struct {
|
||||||
Now int `db:"NOW"`
|
Now int `db:"NOW"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Stat - status
|
||||||
|
type Stat struct {
|
||||||
|
Total int
|
||||||
|
Online int
|
||||||
|
Offline int
|
||||||
|
Known int
|
||||||
|
Unknown int
|
||||||
|
}
|
||||||
|
|
||||||
// GuiData - all data sent to html page
|
// GuiData - all data sent to html page
|
||||||
type GuiData struct {
|
type GuiData struct {
|
||||||
Config Conf
|
Config Conf
|
||||||
|
|
|
||||||
|
|
@ -90,10 +90,45 @@ func apiEdit(c *gin.Context) {
|
||||||
c.IndentedJSON(http.StatusOK, "OK")
|
c.IndentedJSON(http.StatusOK, "OK")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testNotifyHandler(c *gin.Context) {
|
func apiNotifyTest(c *gin.Context) {
|
||||||
|
|
||||||
msg := "Test notification from WatchYourLAN"
|
msg := "WatchYourLAN: test notification"
|
||||||
notify.Shout(msg, appConfig.ShoutURL)
|
notify.Shout(msg, appConfig.ShoutURL)
|
||||||
|
|
||||||
c.Status(http.StatusOK)
|
c.Status(http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func apiStatus(c *gin.Context) {
|
||||||
|
var status models.Stat
|
||||||
|
var searchHosts []models.Host
|
||||||
|
|
||||||
|
iface := c.Param("iface")
|
||||||
|
iface = iface[1:]
|
||||||
|
|
||||||
|
if iface != "" {
|
||||||
|
for _, host := range allHosts {
|
||||||
|
if iface == host.Iface {
|
||||||
|
searchHosts = append(searchHosts, host)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
searchHosts = allHosts
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, host := range searchHosts {
|
||||||
|
status.Total = status.Total + 1
|
||||||
|
|
||||||
|
if host.Known > 0 {
|
||||||
|
status.Known = status.Known + 1
|
||||||
|
} else {
|
||||||
|
status.Unknown = status.Unknown + 1
|
||||||
|
}
|
||||||
|
if host.Now > 0 {
|
||||||
|
status.Online = status.Online + 1
|
||||||
|
} else {
|
||||||
|
status.Offline = status.Offline + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.IndentedJSON(http.StatusOK, status)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
async function testNotifications() {
|
async function testNotifications() {
|
||||||
|
|
||||||
const url = '/api/test_notify'
|
const url = '/api/notify_test';
|
||||||
await fetch(url, { method: 'post' })
|
await fetch(url);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -77,7 +77,7 @@ func compareHosts(foundHosts []models.Host) {
|
||||||
|
|
||||||
fHost.Name, fHost.DNS = updateDNS(fHost)
|
fHost.Name, fHost.DNS = updateDNS(fHost)
|
||||||
|
|
||||||
msg := fmt.Sprintf("Unknown host Names: '%s', IP: '%s', MAC: '%s', Hw: '%s', Iface: '%s'", fHost.DNS, fHost.IP, fHost.Mac, fHost.Hw, fHost.Iface)
|
msg := fmt.Sprintf("WatchYourLAN: unknown host found. Names: '%s', IP: '%s', MAC: '%s', Hw: '%s', Iface: '%s'", fHost.DNS, fHost.IP, fHost.Mac, fHost.Hw, fHost.Iface)
|
||||||
slog.Warn(msg)
|
slog.Warn(msg)
|
||||||
notify.Shout(msg, appConfig.ShoutURL) // Notify through Shoutrrr
|
notify.Shout(msg, appConfig.ShoutURL) // Notify through Shoutrrr
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><button type="submit" class="btn btn-primary">Save</button></td>
|
<td><button type="submit" class="btn btn-primary">Save</button></td>
|
||||||
<td><button type="button" style="float: right;" class="btn btn-success" onclick="testNotifications()">Test notifications</button></td>
|
<td><button type="button" style="float: right;" class="btn btn-info" onclick="testNotifications()">Test notification</button></td>
|
||||||
<td></td>
|
<td></td>
|
||||||
</tr>
|
</tr>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
||||||
|
|
@ -46,13 +46,14 @@ func Gui(dirPath, nodePath string) {
|
||||||
|
|
||||||
router.StaticFS("/fs/", http.FS(pubFS)) // public
|
router.StaticFS("/fs/", http.FS(pubFS)) // public
|
||||||
|
|
||||||
router.GET("/api/all", apiAll) // api.go
|
router.GET("/api/all", apiAll) // api.go
|
||||||
router.GET("/api/edit/:id/:name/*known", apiEdit) // api.go
|
router.GET("/api/edit/:id/:name/*known", apiEdit) // api.go
|
||||||
router.GET("/api/history/*mac", apiHistory) // api.go
|
router.GET("/api/history/*mac", apiHistory) // api.go
|
||||||
router.GET("/api/host/:id", apiHost) // api.go
|
router.GET("/api/host/:id", apiHost) // api.go
|
||||||
router.GET("/api/host/del/:id", apiHostDel) // api.go
|
router.GET("/api/host/del/:id", apiHostDel) // api.go
|
||||||
router.GET("/api/port/:addr/:port", apiPort) // api.go
|
router.GET("/api/notify_test", apiNotifyTest) // api.go
|
||||||
router.POST("/api/test_notify", testNotifyHandler) // api.go
|
router.GET("/api/port/:addr/:port", apiPort) // api.go
|
||||||
|
router.GET("/api/status/*iface", apiStatus) // api.go
|
||||||
|
|
||||||
router.GET("/", indexHandler) // index.go
|
router.GET("/", indexHandler) // index.go
|
||||||
router.GET("/history/", historyHandler) // index.go
|
router.GET("/history/", historyHandler) // index.go
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue