adding a few more vars, setting collection to 60 secs

This commit is contained in:
2023-09-25 22:59:36 -04:00
parent 28df368156
commit 3c9149f7c0
8 changed files with 117 additions and 3 deletions

24
.dockerignore Normal file
View File

@@ -0,0 +1,24 @@
.git
.cache
.vscode
.devcontainer
builds
config
database
documents
done
engine
node_modules
public/node_modules
public/src
public/.cache
*.bleve
.gitattributes
.gitignore
database.txt
go.mod
go.sum
*.db
*.log
package.json
yarn.lock

2
.gitignore vendored
View File

@@ -21,3 +21,5 @@
# Go workspace file
go.work
bin/*
.env

21
Dockerfile Normal file
View File

@@ -0,0 +1,21 @@
# Stage 1
FROM alpine:latest as build
RUN mkdir -p /opt/prometheus-plex-exporter/public/built && \
mkdir /opt/prometheus-plex-exporter/config && \
adduser -S prom && addgroup -S prom
WORKDIR /opt/prometheus-plex-exporter
COPY LICENSE README.md /opt/prometheus-plex-exporter/config/
COPY bin/prometheus-plex-exporter /opt/prometheus-plex-exporter/prometheus-plex-exporter
RUN chmod +x /opt/prometheus-plex-exporter/prometheus-plex-exporter && \
chown -R prom:prom /opt/prometheus-plex-exporter/
# Stage 2
FROM scratch
COPY --from=build / /
LABEL Author="deranjer"
LABEL name="prometheus-plex-exporter"
EXPOSE 9545
WORKDIR /opt/prometheus-plex-exporter
ENTRYPOINT [ "/opt/prometheus-plex-exporter/prometheus-plex-exporter" ]
#docker build -t deranjer/goedms:latest .

22
Taskfile.yml Normal file
View File

@@ -0,0 +1,22 @@
version: '3'
dotenv: ['.env']
tasks:
build:
cmds:
- GOOS=linux GOARCH=amd64 go build -o bin/prometheus-plex-exporter . # The infamous windows .
build-docker:
cmds:
- task: build
- docker build -t gitea.derajnet.duckdns.org/deranjer/prometheus-plex-exporter:latest .
publish-docker:
dotenv: ['.env']
cmds:
- task: build
- task: build-docker
- docker login gitea.derajnet.duckdns.org -u deranjer -p $DOCKER_PASS
- docker push gitea.derajnet.duckdns.org/deranjer/prometheus-plex-exporter:latest
test-env:
cmds:
- echo docker login -u deranjer -p $DOCKER_PASS

View File

@@ -72,15 +72,19 @@ func main() {
prometheus.MustRegister(plexActiveSessions)
prometheus.MustRegister(plexNumMovies)
prometheus.MustRegister(plexNumTVShows)
prometheus.MustRegister(plexTranscodeSessions)
prometheus.MustRegister(plexAllSessions)
go func() {
for {
time.Sleep(time.Second * 5)
time.Sleep(time.Second * 60)
fmt.Println("Collecting info...")
stats := pc.gatherAllStats()
plexActiveSessions.Set(stats.currentSessions)
plexNumMovies.Set(stats.numMovies)
plexNumTVShows.Set(stats.numTV)
plexTranscodeSessions.Set(stats.numTranscodes)
plexAllSessions.Set(stats.numAllSessions)
}
}()

View File

@@ -13,6 +13,8 @@ type plexStats struct {
currentSessions float64
numMovies float64
numTV float64
numTranscodes float64
numAllSessions float64
}
type PlexClient struct {
@@ -76,7 +78,7 @@ func (pc *PlexClient) getNumMovies() *ActiveSessions {
libraryAll := ActiveSessions{}
err := json.Unmarshal(result, &libraryAll)
if err != nil {
fmt.Println("Error unmarshalling current active sessions: ", err)
fmt.Println("Error unmarshalling num movies: ", err)
}
return &libraryAll
}
@@ -86,20 +88,45 @@ func (pc *PlexClient) getNumTV() *ActiveSessions {
libraryAll := ActiveSessions{}
err := json.Unmarshal(result, &libraryAll)
if err != nil {
fmt.Println("Error unmarshalling current active sessions: ", err)
fmt.Println("Error unmarshalling num tv: ", err)
}
return &libraryAll
}
func (pc *PlexClient) getNumTranscodes() *RawPlexModel {
result := pc.sendRequest("/transcode/sessions", nil)
transcodesAll := RawPlexModel{}
err := json.Unmarshal(result, &transcodesAll)
if err != nil {
fmt.Println("Error unmarshalling num transcodes: ", err)
}
return &transcodesAll
}
// getServerHistory gets the entire history of plays on the plex server
func (pc *PlexClient) getServerHistory() *RawPlexModel {
result := pc.sendRequest("/status/sessions/history/all", nil) //Hard code library ID for now TODO: Grab from library/sections call
sessionsAll := RawPlexModel{}
err := json.Unmarshal(result, &sessionsAll)
if err != nil {
fmt.Println("Error unmarshalling session server history: ", err)
}
return &sessionsAll
}
func (pc *PlexClient) gatherAllStats() *plexStats {
allStats := plexStats{}
sessionsData := pc.getActiveSessions()
libraryDetails := pc.getNumMovies()
tvshowDetails := pc.getNumTV()
transcodeDetails := pc.getNumTranscodes()
allSessionsData := pc.getServerHistory()
allStats.currentSessions = float64(sessionsData.MediaContainer.Size)
allStats.numMovies = float64(libraryDetails.MediaContainer.Size)
allStats.numTV = float64(tvshowDetails.MediaContainer.Size)
allStats.numTranscodes = float64(transcodeDetails.MediaContainer.Size)
allStats.numAllSessions = float64(allSessionsData.MediaContainer.Size)
return &allStats
}

View File

@@ -1,5 +1,11 @@
package main
type RawPlexModel struct {
MediaContainer struct {
Size int `json:"size"`
}
}
type ActiveSessions struct {
MediaContainer struct {
Size int `json:"size"`

View File

@@ -12,3 +12,11 @@ var plexNumMovies = prometheus.NewGauge(prometheus.GaugeOpts{
var plexNumTVShows = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "plex_num_tv_shows", Help: "Number of TV Shows in the plex database",
})
var plexTranscodeSessions = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "plex_num_transcodes", Help: "Number of current transcodes occuring in plex",
})
var plexAllSessions = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "plex_num_all_sessions", Help: "Number of all sessions history for server",
})