diff --git a/collector/plex_collector.go b/collector/plex_collector.go index f40cb6a..032e6da 100644 --- a/collector/plex_collector.go +++ b/collector/plex_collector.go @@ -7,11 +7,15 @@ import ( ) type PlexCollector struct { - Logger *log.Entry - client *plex.PlexClient - serverInfo *prometheus.Desc - sessionsMetric *prometheus.Desc - libraryMetric *prometheus.Desc + Logger *log.Entry + client *plex.PlexClient + serverInfo *prometheus.Desc + sessionsMetric *prometheus.Desc + libraryMetric *prometheus.Desc + showLibraryMetric *prometheus.Desc + showLibrarySeasonMetric *prometheus.Desc + showLibraryEpisodeMetric *prometheus.Desc + showLibraryWatchedMetric *prometheus.Desc } func NewPlexCollector(c *plex.PlexClient, l *log.Entry) *PlexCollector { @@ -30,6 +34,22 @@ func NewPlexCollector(c *plex.PlexClient, l *log.Entry) *PlexCollector { "Number of items in a library section", []string{"server_name", "server_id", "name", "type"}, nil, ), + showLibraryMetric: prometheus.NewDesc("plex_library_section_show_count", + "Number of shows in a library section of type show", + []string{"server_name", "server_id", "name"}, nil, + ), + showLibrarySeasonMetric: prometheus.NewDesc("plex_library_section_show_season_count", + "Number of seasons in a library section of type show", + []string{"server_name", "server_id", "name"}, nil, + ), + showLibraryEpisodeMetric: prometheus.NewDesc("plex_library_section_show_episode_count", + "Number of episodes in a library section of type show", + []string{"server_name", "server_id", "name"}, nil, + ), + showLibraryWatchedMetric: prometheus.NewDesc("plex_library_section_show_watched_count", + "Number of watched episodes in a library section of type show", + []string{"server_name", "server_id", "name"}, nil, + ), } } @@ -37,6 +57,10 @@ func (c *PlexCollector) Describe(ch chan<- *prometheus.Desc) { ch <- c.serverInfo ch <- c.sessionsMetric ch <- c.libraryMetric + ch <- c.showLibraryMetric + ch <- c.showLibrarySeasonMetric + ch <- c.showLibraryEpisodeMetric + ch <- c.showLibraryWatchedMetric } func (c *PlexCollector) Collect(ch chan<- prometheus.Metric) { @@ -50,5 +74,12 @@ func (c *PlexCollector) Collect(ch chan<- prometheus.Metric) { for _, l := range v.Libraries { ch <- prometheus.MustNewConstMetric(c.libraryMetric, prometheus.GaugeValue, float64(l.Size), v.Name, v.ID, l.Name, l.Type) } + for _, s := range v.ShowLibraries { + ch <- prometheus.MustNewConstMetric(c.libraryMetric, prometheus.GaugeValue, float64(s.ShowSize), v.Name, v.ID, s.Name, s.Type) + ch <- prometheus.MustNewConstMetric(c.showLibraryMetric, prometheus.GaugeValue, float64(s.ShowSize), v.Name, v.ID, s.Name) + ch <- prometheus.MustNewConstMetric(c.showLibrarySeasonMetric, prometheus.GaugeValue, float64(s.SeasonSize), v.Name, v.ID, s.Name) + ch <- prometheus.MustNewConstMetric(c.showLibraryEpisodeMetric, prometheus.GaugeValue, float64(s.EpisodeSize), v.Name, v.ID, s.Name) + ch <- prometheus.MustNewConstMetric(c.showLibraryWatchedMetric, prometheus.GaugeValue, float64(s.WatchedSize), v.Name, v.ID, s.Name) + } } } diff --git a/plex/api/library.go b/plex/api/library.go index a6c0c5e..ce1f976 100644 --- a/plex/api/library.go +++ b/plex/api/library.go @@ -22,3 +22,18 @@ type SectionResponse struct { type SectionDetail struct { TotalSize int `json:"totalSize"` } + +type ShowSectionResponse struct { + ShowSectionDetail `json:"MediaContainer"` +} + +type ShowSectionDetail struct { + ShowCount int `json:"size"` + Shows []ShowDetail `json:"MetaData"` +} + +type ShowDetail struct { + EpisodeCount int `json:"leafCount"` + WatchedEpisodeCount int `json:"viewedLeafCount"` + SeasonCount int `json:"childCount"` +} diff --git a/plex/client.go b/plex/client.go index 87ed6b3..e653d3c 100644 --- a/plex/client.go +++ b/plex/client.go @@ -95,18 +95,37 @@ func (c *PlexClient) GetServerMetrics() map[string]ServerMetric { if err != nil { logger.Debugf("Could not convert sections ID to int. (%s)", section.ID) } - size, err := server.GetSectionSize(id) - if err != nil { - logger.Debugf("Could not get section size for \"%s\": %s", section.Name, err) - continue - } - libraryMetric := LibraryMetric{ - Name: section.Name, - Type: section.Type, - Size: size, + if section.Type == "show" { + showCount, seasonCount, episodeCount, watchedEpisodeCount, err := server.GetShowSectionSize(id) + if err != nil { + logger.Debugf("Could not get show section sizes for \"%s\": %s", section.Name, err) + continue + } + showLibraryMetrics := ShowLibraryMetric{ + Name: section.Name, + Type: section.Type, + ShowSize: showCount, + SeasonSize: seasonCount, + EpisodeSize: episodeCount, + WatchedSize: watchedEpisodeCount, + } + + serverMetric.ShowLibraries = append(serverMetric.ShowLibraries, showLibraryMetrics) + } else { + size, err := server.GetSectionSize(id) + if err != nil { + logger.Debugf("Could not get section size for \"%s\": %s", section.Name, err) + continue + } + libraryMetric := LibraryMetric{ + Name: section.Name, + Type: section.Type, + Size: size, + } + + serverMetric.Libraries = append(serverMetric.Libraries, libraryMetric) } - serverMetric.Libraries = append(serverMetric.Libraries, libraryMetric) } serverMap[server.Name] = serverMetric diff --git a/plex/server.go b/plex/server.go index 7145364..89bb63f 100644 --- a/plex/server.go +++ b/plex/server.go @@ -128,6 +128,29 @@ func (s *Server) GetSectionSize(id int) (int, error) { return sectionResponse.TotalSize, nil } +func (s *Server) GetShowSectionSize(id int) (int, int, int, int, error) { + + sectionResponse := api.ShowSectionResponse{} + + _, body, err := sendRequest("GET", fmt.Sprintf(SectionURI, s.BaseURL, id), headers, s.httpClient) + if err != nil { + return -1, -1, -1, -1, err + } + + err = json.Unmarshal(body, §ionResponse) + if err != nil { + return -1, -1, -1, -1, err + } + seasonCount, episodeCount, watchedEpisodeCount := 0, 0, 0 + for _, show := range sectionResponse.Shows { + seasonCount += show.SeasonCount + episodeCount += show.EpisodeCount + watchedEpisodeCount += show.WatchedEpisodeCount + } + + return sectionResponse.ShowCount, seasonCount, episodeCount, watchedEpisodeCount, nil +} + func (s *Server) get(url string) ([]byte, error) { _, body, err := sendRequest("GET", url, s.headers, s.httpClient) return body, err diff --git a/plex/server_metric.go b/plex/server_metric.go index 81673d2..ce7692d 100644 --- a/plex/server_metric.go +++ b/plex/server_metric.go @@ -7,6 +7,7 @@ type ServerMetric struct { Platform string ActiveSessions int Libraries []LibraryMetric + ShowLibraries []ShowLibraryMetric } type LibraryMetric struct { @@ -14,3 +15,12 @@ type LibraryMetric struct { Type string Size int } + +type ShowLibraryMetric struct { + Name string + Type string + ShowSize int + SeasonSize int + EpisodeSize int + WatchedSize int +}