forked from zabbix-tools/go-zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_cache.go
124 lines (100 loc) · 2.97 KB
/
file_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package zabbix
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"time"
)
/*
cachedSessionData represents a model of cached session.
Example:
{
"createdAt": 1530056885,
"session": {
"url": "...",
"token": "...",
"apiVersion": "..."
}
}
*/
type cachedSessionContainer struct {
CreatedAt int64 `json:"createdAt"`
Session `json:"session"`
}
// SessionFileCache is Zabbix session filesystem cache.
type SessionFileCache struct {
filePath string
sessionLifeTime time.Duration
filePermissions uint32
}
// SetFilePath sets Zabbix session cache file path. Default value is "./zabbix_session"
func (c *SessionFileCache) SetFilePath(filePath string) *SessionFileCache {
c.filePath = filePath
return c
}
// SetFilePermissions sets permissions for a session file. Default value is 0655.
func (c *SessionFileCache) SetFilePermissions(permissions uint32) *SessionFileCache {
c.filePermissions = permissions
return c
}
// SetSessionLifetime sets lifetime in seconds of cached Zabbix session. Default value is 4 hours.
func (c *SessionFileCache) SetSessionLifetime(d time.Duration) {
c.sessionLifeTime = d
}
// SaveSession saves session to a cache
func (c *SessionFileCache) SaveSession(session *Session) error {
sessionContainer := cachedSessionContainer{
CreatedAt: time.Now().Unix(),
Session: *session,
}
serialized, err := json.Marshal(sessionContainer)
if err != nil {
return err
}
return ioutil.WriteFile(c.filePath, []byte(serialized), os.FileMode(c.filePermissions))
}
// GetSession returns cached Zabbix session
func (c *SessionFileCache) GetSession() (*Session, error) {
contents, err := ioutil.ReadFile(c.filePath)
if err != nil {
return nil, err
}
var sessionContainer cachedSessionContainer
if err := json.Unmarshal(contents, &sessionContainer); err != nil {
return nil, err
}
// Check if session is expired
if !c.checkSessionLifeTime(&sessionContainer) {
// Delete the session file and throw an error if TTL is expired
os.Remove(c.filePath)
return nil, fmt.Errorf("cached session lifetime expired")
}
return &sessionContainer.Session, err
}
// checkSessionLifeTime checks if session is still actual
func (c *SessionFileCache) checkSessionLifeTime(sessionContainer *cachedSessionContainer) bool {
now := time.Now().Unix()
createdAt := sessionContainer.CreatedAt
timeDiff := now - createdAt
// Check session TTL by time diff
isExpired := timeDiff > int64(c.sessionLifeTime)
return !isExpired
}
// HasSession checks if any valid Zabbix session has been cached and available
func (c *SessionFileCache) HasSession() bool {
_, err := os.Stat(c.filePath)
return err == nil
}
// Flush removes a cached session
func (c *SessionFileCache) Flush() error {
return os.Remove(c.filePath)
}
// NewSessionFileCache creates a new instance of session file system cache
func NewSessionFileCache() *SessionFileCache {
return &SessionFileCache{
filePath: "./zabbix_session",
sessionLifeTime: 14400, // Default TTL is 4 hours
filePermissions: 0600,
}
}