| 1 | package serverinfo |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "encoding/xml" |
| 6 | "fmt" |
| 7 | "strconv" |
| 8 | ) |
| 9 | |
| 10 | // ServerInfo contains the complete data received from the server. |
| 11 | type ServerInfo struct { |
| 12 | Meta Meta `json:"meta"` |
| 13 | Data Data `json:"data"` |
| 14 | } |
| 15 | |
| 16 | // Meta contains meta information about the result. |
| 17 | type Meta struct { |
| 18 | Status string `json:"status"` |
| 19 | StatusCode int `json:"statuscode"` |
| 20 | Message string `json:"message"` |
| 21 | } |
| 22 | |
| 23 | // Data contains the status information about the instance. |
| 24 | type Data struct { |
| 25 | Nextcloud Nextcloud `json:"nextcloud"` |
| 26 | Server Server `json:"server"` |
| 27 | ActiveUsers ActiveUsers `json:"activeUsers"` |
| 28 | } |
| 29 | |
| 30 | // Nextcloud contains information about the nextcloud installation. |
| 31 | type Nextcloud struct { |
| 32 | System System `json:"system"` |
| 33 | Storage Storage `json:"storage"` |
| 34 | Shares Shares `json:"shares"` |
| 35 | } |
| 36 | |
| 37 | // System contains nextcloud configuration and system information. |
| 38 | type System struct { |
| 39 | Version string `json:"version"` |
| 40 | Theme string `json:"theme"` |
| 41 | EnableAvatars bool `json:"enable_avatars"` |
| 42 | EnablePreviews bool `json:"enable_previews"` |
| 43 | MemcacheLocal string `json:"memcache.local"` |
| 44 | MemcacheDistributed string `json:"memcache.distributed"` |
| 45 | MemcacheLocking string `json:"memcache.locking"` |
| 46 | FilelockingEnabled bool `json:"filelocking.enabled"` |
| 47 | Debug bool `json:"debug"` |
| 48 | FreeSpace float64 `json:"freespace"` |
| 49 | Apps Apps `json:"apps"` |
| 50 | Update Update `json:"update"` |
| 51 | } |
| 52 | |
| 53 | const boolYes = "yes" |
| 54 | |
| 55 | func (s *System) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { |
| 56 | var raw struct { |
| 57 | Version string `xml:"version"` |
| 58 | Theme string `xml:"theme"` |
| 59 | EnableAvatars string `xml:"enable_avatars"` |
| 60 | EnablePreviews string `xml:"enable_previews"` |
| 61 | MemcacheLocal string `xml:"memcache.local"` |
| 62 | MemcacheDistributed string `xml:"memcache.distributed"` |
| 63 | MemcacheLocking string `xml:"memcache.locking"` |
| 64 | FilelockingEnabled string `xml:"filelocking.enabled"` |
| 65 | Debug string `xml:"debug"` |
| 66 | FreeSpace float64 `xml:"freespace"` |
| 67 | Apps Apps `xml:"apps"` |
| 68 | Update Update `xml:"update"` |
| 69 | } |
| 70 | if err := d.DecodeElement(&raw, &start); err != nil { |
| 71 | return err |
| 72 | } |
| 73 | s.Version = raw.Version |
| 74 | s.Theme = raw.Theme |
| 75 | s.EnableAvatars = raw.EnableAvatars == boolYes |
| 76 | s.EnablePreviews = raw.EnablePreviews == boolYes |
| 77 | s.MemcacheLocal = raw.MemcacheLocal |
| 78 | s.MemcacheDistributed = raw.MemcacheDistributed |
| 79 | s.MemcacheLocking = raw.MemcacheLocking |
| 80 | s.FilelockingEnabled = raw.FilelockingEnabled == boolYes |
| 81 | s.Debug = raw.Debug == boolYes |
| 82 | s.FreeSpace = raw.FreeSpace |
| 83 | s.Apps = raw.Apps |
| 84 | s.Update = raw.Update |
| 85 | return nil |
| 86 | } |
| 87 | |
| 88 | func (s *System) UnmarshalJSON(data []byte) error { |
| 89 | var raw struct { |
| 90 | Version string `json:"version"` |
| 91 | Theme string `json:"theme"` |
| 92 | EnableAvatars string `json:"enable_avatars"` |
| 93 | EnablePreviews string `json:"enable_previews"` |
| 94 | MemcacheLocal string `json:"memcache.local"` |
| 95 | MemcacheDistributed string `json:"memcache.distributed"` |
| 96 | MemcacheLocking string `json:"memcache.locking"` |
| 97 | FilelockingEnabled string `json:"filelocking.enabled"` |
| 98 | Debug string `json:"debug"` |
| 99 | FreeSpace float64 `json:"freespace"` |
| 100 | Apps Apps `json:"apps"` |
| 101 | Update Update `json:"update"` |
| 102 | } |
| 103 | if err := json.Unmarshal(data, &raw); err != nil { |
| 104 | return err |
| 105 | } |
| 106 | s.Version = raw.Version |
| 107 | s.Theme = raw.Theme |
| 108 | s.EnableAvatars = raw.EnableAvatars == boolYes |
| 109 | s.EnablePreviews = raw.EnablePreviews == boolYes |
| 110 | s.MemcacheLocal = raw.MemcacheLocal |
| 111 | s.MemcacheDistributed = raw.MemcacheDistributed |
| 112 | s.MemcacheLocking = raw.MemcacheLocking |
| 113 | s.FilelockingEnabled = raw.FilelockingEnabled == boolYes |
| 114 | s.Debug = raw.Debug == boolYes |
| 115 | s.FreeSpace = raw.FreeSpace |
| 116 | s.Apps = raw.Apps |
| 117 | s.Update = raw.Update |
| 118 | return nil |
| 119 | } |
| 120 | |
| 121 | // Apps contains information about installed apps and updates. |
| 122 | type Apps struct { |
| 123 | Installed uint `json:"num_installed"` |
| 124 | AvailableUpdates uint `json:"num_updates_available"` |
| 125 | } |
| 126 | |
| 127 | // Update contains information about Nextcloud system updates. |
| 128 | type Update struct { |
| 129 | Available bool `json:"available"` |
| 130 | AvailableVersion string `json:"available_version"` |
| 131 | } |
| 132 | |
| 133 | // Storage contains information about the nextcloud storage system. |
| 134 | type Storage struct { |
| 135 | Users uint `json:"num_users"` |
| 136 | Files uint `json:"num_files"` |
| 137 | Storages uint `json:"num_storages"` |
| 138 | StoragesLocal uint `json:"num_storages_local"` |
| 139 | StoragesHome uint `json:"num_storages_home"` |
| 140 | StoragesOther uint `json:"num_storages_other"` |
| 141 | } |
| 142 | |
| 143 | // Shares contains information about nextcloud shares. |
| 144 | type Shares struct { |
| 145 | SharesTotal uint `json:"num_shares"` |
| 146 | SharesUser uint `json:"num_shares_user"` |
| 147 | SharesGroups uint `json:"num_shares_groups"` |
| 148 | SharesLink uint `json:"num_shares_link"` |
| 149 | SharesLinkNoPassword uint `json:"num_shares_link_no_password"` |
| 150 | SharesMail uint `json:"num_shares_mail"` |
| 151 | SharesRoom uint `json:"num_shares_room"` |
| 152 | FedSent uint `json:"num_fed_shares_sent"` |
| 153 | FedReceived uint `json:"num_fed_shares_received"` |
| 154 | // <permissions_0_1>2</permissions_0_1> |
| 155 | // <permissions_3_1>4</permissions_3_1> |
| 156 | // <permissions_0_15>2</permissions_0_15> |
| 157 | // <permissions_3_15>2</permissions_3_15> |
| 158 | // <permissions_0_31>3</permissions_0_31> |
| 159 | // <permissions_1_31>1</permissions_1_31> |
| 160 | } |
| 161 | |
| 162 | // Server contains information about the servers running nextcloud. |
| 163 | type Server struct { |
| 164 | Webserver string `json:"webserver"` |
| 165 | PHP PHP `json:"php"` |
| 166 | Database Database `json:"database"` |
| 167 | } |
| 168 | |
| 169 | // PHP contains information about the PHP installation. |
| 170 | type PHP struct { |
| 171 | Version string `json:"version"` |
| 172 | MemoryLimit int64 `json:"memory_limit"` |
| 173 | MaxExecutionTime uint `json:"max_execution_time"` |
| 174 | UploadMaxFilesize int64 `json:"upload_max_filesize"` |
| 175 | } |
| 176 | |
| 177 | // Database contains information about the database used by nextcloud. |
| 178 | type Database struct { |
| 179 | Type string `json:"type"` |
| 180 | Version string `json:"version"` |
| 181 | Size uint64 `json:"size"` |
| 182 | } |
| 183 | |
| 184 | func (d *Database) UnmarshalJSON(data []byte) error { |
| 185 | var raw struct { |
| 186 | Type string `json:"type"` |
| 187 | Version string `json:"version"` |
| 188 | Size interface{} `json:"size"` |
| 189 | } |
| 190 | |
| 191 | if err := json.Unmarshal(data, &raw); err != nil { |
| 192 | return err |
| 193 | } |
| 194 | |
| 195 | d.Type = raw.Type |
| 196 | d.Version = raw.Version |
| 197 | |
| 198 | switch rawSize := raw.Size.(type) { |
| 199 | case float64: |
| 200 | if rawSize < 0 { |
| 201 | return fmt.Errorf("negative value for database.size: %f", rawSize) |
| 202 | } |
| 203 | d.Size = uint64(rawSize) |
| 204 | case string: |
| 205 | parsedSize, err := strconv.ParseUint(rawSize, 10, 64) |
| 206 | if err != nil { |
| 207 | return fmt.Errorf("can not parse database.size %q: %w", rawSize, err) |
| 208 | } |
| 209 | d.Size = parsedSize |
| 210 | default: |
| 211 | return fmt.Errorf("unexpected type for database.size: %t", rawSize) |
| 212 | } |
| 213 | |
| 214 | return nil |
| 215 | } |
| 216 | |
| 217 | // ActiveUsers contains statistics about the active users. |
| 218 | type ActiveUsers struct { |
| 219 | Last5Minutes uint `json:"last5minutes"` |
| 220 | LastHour uint `json:"last1hour"` |
| 221 | LastDay uint `json:"last24hours"` |
| 222 | } |