Skip to content
This repository was archived by the owner on Mar 10, 2025. It is now read-only.

Add tv show series count #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
41 changes: 36 additions & 5 deletions collector/plex_collector.go
Original file line number Diff line number Diff line change
@@ -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,13 +34,33 @@ 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,
),
}
}

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)
}
}
}
15 changes: 15 additions & 0 deletions plex/api/library.go
Original file line number Diff line number Diff line change
@@ -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"`
}
39 changes: 29 additions & 10 deletions plex/client.go
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions plex/server.go
Original file line number Diff line number Diff line change
@@ -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, &sectionResponse)
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
10 changes: 10 additions & 0 deletions plex/server_metric.go
Original file line number Diff line number Diff line change
@@ -7,10 +7,20 @@ type ServerMetric struct {
Platform string
ActiveSessions int
Libraries []LibraryMetric
ShowLibraries []ShowLibraryMetric
}

type LibraryMetric struct {
Name string
Type string
Size int
}

type ShowLibraryMetric struct {
Name string
Type string
ShowSize int
SeasonSize int
EpisodeSize int
WatchedSize int
}