Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error parsing large values for free space #114

Merged
merged 4 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- App-related metrics (installed, available updates) are opt-in now, mirroring the change in Nextcloud 28

### Fixed

- Error parsing "free space" reported by Nextcloud when number is very large

## [0.6.2] - 2023-10-15

### Changed
Expand Down
2 changes: 1 addition & 1 deletion internal/metrics/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func collectSimpleMetrics(ch chan<- prometheus.Metric, status *serverinfo.Server
},
{
desc: freeSpaceDesc,
value: float64(status.Data.Nextcloud.System.FreeSpace),
value: status.Data.Nextcloud.System.FreeSpace,
},
{
desc: activeUsersDesc,
Expand Down
1 change: 1 addition & 0 deletions serverinfo/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func TestParseJSON(t *testing.T) {
"negative-space.json",
"na-values.json",
"nc22.json",
"large-freespace.json",
}

for _, inputFile := range inputFiles {
Expand Down
66 changes: 33 additions & 33 deletions serverinfo/serverinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,34 @@ type Nextcloud struct {

// System contains nextcloud configuration and system information.
type System struct {
Version string `json:"version"`
Theme string `json:"theme"`
EnableAvatars bool `json:"enable_avatars"`
EnablePreviews bool `json:"enable_previews"`
MemcacheLocal string `json:"memcache.local"`
MemcacheDistributed string `json:"memcache.distributed"`
MemcacheLocking string `json:"memcache.locking"`
FilelockingEnabled bool `json:"filelocking.enabled"`
Debug bool `json:"debug"`
FreeSpace int64 `json:"freespace"`
Apps Apps `json:"apps"`
Version string `json:"version"`
Theme string `json:"theme"`
EnableAvatars bool `json:"enable_avatars"`
EnablePreviews bool `json:"enable_previews"`
MemcacheLocal string `json:"memcache.local"`
MemcacheDistributed string `json:"memcache.distributed"`
MemcacheLocking string `json:"memcache.locking"`
FilelockingEnabled bool `json:"filelocking.enabled"`
Debug bool `json:"debug"`
FreeSpace float64 `json:"freespace"`
Apps Apps `json:"apps"`
}

const boolYes = "yes"

func (s *System) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var raw struct {
Version string `xml:"version"`
Theme string `xml:"theme"`
EnableAvatars string `xml:"enable_avatars"`
EnablePreviews string `xml:"enable_previews"`
MemcacheLocal string `xml:"memcache.local"`
MemcacheDistributed string `xml:"memcache.distributed"`
MemcacheLocking string `xml:"memcache.locking"`
FilelockingEnabled string `xml:"filelocking.enabled"`
Debug string `xml:"debug"`
FreeSpace int64 `xml:"freespace"`
Apps Apps `xml:"apps"`
Version string `xml:"version"`
Theme string `xml:"theme"`
EnableAvatars string `xml:"enable_avatars"`
EnablePreviews string `xml:"enable_previews"`
MemcacheLocal string `xml:"memcache.local"`
MemcacheDistributed string `xml:"memcache.distributed"`
MemcacheLocking string `xml:"memcache.locking"`
FilelockingEnabled string `xml:"filelocking.enabled"`
Debug string `xml:"debug"`
FreeSpace float64 `xml:"freespace"`
Apps Apps `xml:"apps"`
}
if err := d.DecodeElement(&raw, &start); err != nil {
return err
Expand All @@ -84,17 +84,17 @@ func (s *System) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {

func (s *System) UnmarshalJSON(data []byte) error {
var raw struct {
Version string `json:"version"`
Theme string `json:"theme"`
EnableAvatars string `json:"enable_avatars"`
EnablePreviews string `json:"enable_previews"`
MemcacheLocal string `json:"memcache.local"`
MemcacheDistributed string `json:"memcache.distributed"`
MemcacheLocking string `json:"memcache.locking"`
FilelockingEnabled string `json:"filelocking.enabled"`
Debug string `json:"debug"`
FreeSpace int64 `json:"freespace"`
Apps Apps `json:"apps"`
Version string `json:"version"`
Theme string `json:"theme"`
EnableAvatars string `json:"enable_avatars"`
EnablePreviews string `json:"enable_previews"`
MemcacheLocal string `json:"memcache.local"`
MemcacheDistributed string `json:"memcache.distributed"`
MemcacheLocking string `json:"memcache.locking"`
FilelockingEnabled string `json:"filelocking.enabled"`
Debug string `json:"debug"`
FreeSpace float64 `json:"freespace"`
Apps Apps `json:"apps"`
}
if err := json.Unmarshal(data, &raw); err != nil {
return err
Expand Down
135 changes: 135 additions & 0 deletions serverinfo/testdata/large-freespace.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
{
"ocs": {
"meta": {
"status": "ok",
"statuscode": 200,
"message": "OK"
},
"data": {
"nextcloud": {
"system": {
"version": "21.0.3.1",
"theme": "",
"enable_avatars": "yes",
"enable_previews": "yes",
"memcache.local": "\\OC\\Memcache\\APCu",
"memcache.distributed": "none",
"filelocking.enabled": "yes",
"memcache.locking": "\\OC\\Memcache\\Redis",
"debug": "no",
"freespace": 9.2233720360673E+18,
"cpuload": [
0.08,
0.05,
0.06
],
"mem_total": 1986232,
"mem_free": 1285532,
"swap_total": 0,
"swap_free": 0,
"apps": {
"num_installed": 42,
"num_updates_available": 0,
"app_updates": []
}
},
"storage": {
"num_users": 4,
"num_files": 148948,
"num_storages": 32,
"num_storages_local": 3,
"num_storages_home": 4,
"num_storages_other": 25
},
"shares": {
"num_shares": 10,
"num_shares_user": 0,
"num_shares_groups": 2,
"num_shares_link": 4,
"num_shares_mail": 1,
"num_shares_room": 0,
"num_shares_link_no_password": 4,
"num_fed_shares_sent": 0,
"num_fed_shares_received": 0,
"permissions_3_1": "2",
"permissions_3_17": "1",
"permissions_4_17": "1",
"permissions_1_31": "2",
"permissions_2_31": "3",
"permissions_3_31": "1"
}
},
"server": {
"webserver": "Apache\/2.4.41 (Ubuntu)",
"php": {
"version": "7.4.3",
"memory_limit": 536870912,
"max_execution_time": 3600,
"upload_max_filesize": 2097152,
"opcache": {
"opcache_enabled": true,
"cache_full": false,
"restart_pending": false,
"restart_in_progress": false,
"memory_usage": {
"used_memory": 35866872,
"free_memory": 98334320,
"wasted_memory": 16536,
"current_wasted_percentage": 0.012320280075073242
},
"interned_strings_usage": {
"buffer_size": 6291008,
"used_memory": 4225688,
"free_memory": 2065320,
"number_of_strings": 68439
},
"opcache_statistics": {
"num_cached_scripts": 1818,
"num_cached_keys": 3489,
"max_cached_keys": 16229,
"hits": 2725757,
"start_time": 1627817478,
"last_restart_time": 0,
"oom_restarts": 0,
"hash_restarts": 0,
"manual_restarts": 0,
"misses": 1830,
"blacklist_misses": 0,
"blacklist_miss_ratio": 0,
"opcache_hit_rate": 99.93290773126576
}
},
"apcu": {
"cache": {
"num_slots": 4099,
"ttl": 0,
"num_hits": 175992,
"num_misses": 1948,
"num_inserts": 2009,
"num_entries": 599,
"expunges": 0,
"start_time": 1627817478,
"mem_size": 295024,
"memory_type": "mmap"
},
"sma": {
"num_seg": 1,
"seg_size": 33554312,
"avail_mem": 33206176
}
}
},
"database": {
"type": "mysql",
"version": "10.5.11",
"size": 59457536
}
},
"activeUsers": {
"last5minutes": 1,
"last1hour": 1,
"last24hours": 2
}
}
}
}
Loading