Skip to content

Commit 6157276

Browse files
author
miktwon
committed
prometheus
1 parent 858c7fd commit 6157276

File tree

9 files changed

+1061
-11
lines changed

9 files changed

+1061
-11
lines changed

fpmConfig/fpmConfig.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package fpmConfig
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"path"
7+
"regexp"
8+
"strings"
9+
10+
"gopkg.in/ini.v1"
11+
)
12+
13+
type Config struct {
14+
Include string
15+
Pools []Pool
16+
}
17+
18+
type Pool struct {
19+
Name string
20+
Listen string
21+
StatusPath string
22+
}
23+
24+
func Parse(fpmConfigPath string) (Config, error) {
25+
c := Config{}
26+
cfg := ini.Empty()
27+
err := cfg.Append(fpmConfigPath)
28+
if err != nil {
29+
return c, err
30+
}
31+
32+
global, err := cfg.GetSection("global")
33+
if err != nil {
34+
return c, err
35+
}
36+
37+
include, err := global.GetKey("include")
38+
if err != nil {
39+
return c, err
40+
}
41+
42+
c.Include = include.Value()
43+
44+
file := regexp.QuoteMeta(path.Base(c.Include))
45+
file = strings.Replace(file, regexp.QuoteMeta("*"), "(.+)", 1)
46+
file = fmt.Sprintf("^%s$", file)
47+
fileRx := regexp.MustCompile(file)
48+
49+
fmpPoolsDir := path.Dir(c.Include)
50+
osFileInfo, err := ioutil.ReadDir(fmpPoolsDir)
51+
if err != nil {
52+
return c, err
53+
}
54+
55+
for _, info := range osFileInfo {
56+
if info.IsDir() || !fileRx.MatchString(info.Name()) {
57+
continue
58+
}
59+
60+
err = cfg.Append(fmt.Sprintf("%s/%s", fmpPoolsDir, info.Name()))
61+
if err != nil {
62+
return c, err
63+
}
64+
}
65+
66+
for _, section := range cfg.Sections() {
67+
_, err = section.GetKey("pm.status_path")
68+
if err != nil {
69+
continue
70+
}
71+
72+
fillPull(&c, cfg, section.Name())
73+
}
74+
75+
return c, err
76+
}
77+
78+
func fillPull(config *Config, iniConfig *ini.File, poolName string) error {
79+
pool := Pool{}
80+
pool.Name = poolName
81+
82+
section, err := iniConfig.GetSection(poolName)
83+
if err != nil {
84+
return err
85+
}
86+
87+
key, err := section.GetKey("listen")
88+
if err != nil {
89+
return err
90+
}
91+
pool.Listen = key.String()
92+
93+
key, err = section.GetKey("pm.status_path")
94+
if err == nil {
95+
pool.StatusPath = key.String()
96+
}
97+
98+
config.Pools = append(config.Pools, pool)
99+
return nil
100+
}

fpmConfig/fpmConfig_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package fpmConfig
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestParse(t *testing.T) {
10+
c, err := Parse("testData/php-fpm.conf")
11+
assert.Nil(t, err)
12+
assert.Equal(t, "testData/php-fpm.d/*.conf", c.Include)
13+
14+
assert.Equal(t, "www", c.Pools[0].Name)
15+
assert.Equal(t, "/run/php-fpm/php-fpm.sock", c.Pools[0].Listen)
16+
assert.Equal(t, "/status", c.Pools[0].StatusPath)
17+
}

fpmConfig/testData/php-fpm.conf

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
;;;;;;;;;;;;;;;;;;;;;
2+
; FPM Configuration ;
3+
;;;;;;;;;;;;;;;;;;;;;
4+
5+
; All relative paths in this configuration file are relative to PHP's install
6+
; prefix (/usr). This prefix can be dynamically changed by using the
7+
; '-p' argument from the command line.
8+
9+
;;;;;;;;;;;;;;;;;;
10+
; Global Options ;
11+
;;;;;;;;;;;;;;;;;;
12+
13+
[global]
14+
; Pid file
15+
; Note: the default prefix is /var
16+
; Default Value: none
17+
;pid = /run/php-fpm/php-fpm.pid
18+
19+
; Error log file
20+
; If it's set to "syslog", log is sent to syslogd instead of being written
21+
; into a local file.
22+
; Note: the default prefix is /var
23+
; Default Value: log/php-fpm.log
24+
error_log = syslog
25+
26+
; syslog_facility is used to specify what type of program is logging the
27+
; message. This lets syslogd specify that messages from different facilities
28+
; will be handled differently.
29+
; See syslog(3) for possible values (ex daemon equiv LOG_DAEMON)
30+
; Default Value: daemon
31+
;syslog.facility = daemon
32+
33+
; syslog_ident is prepended to every message. If you have multiple FPM
34+
; instances running on the same server, you can change the default value
35+
; which must suit common needs.
36+
; Default Value: php-fpm
37+
;syslog.ident = php-fpm
38+
39+
; Log level
40+
; Possible Values: alert, error, warning, notice, debug
41+
; Default Value: notice
42+
;log_level = notice
43+
44+
; If this number of child processes exit with SIGSEGV or SIGBUS within the time
45+
; interval set by emergency_restart_interval then FPM will restart. A value
46+
; of '0' means 'Off'.
47+
; Default Value: 0
48+
;emergency_restart_threshold = 0
49+
50+
; Interval of time used by emergency_restart_interval to determine when
51+
; a graceful restart will be initiated. This can be useful to work around
52+
; accidental corruptions in an accelerator's shared memory.
53+
; Available Units: s(econds), m(inutes), h(ours), or d(ays)
54+
; Default Unit: seconds
55+
; Default Value: 0
56+
;emergency_restart_interval = 0
57+
58+
; Time limit for child processes to wait for a reaction on signals from master.
59+
; Available units: s(econds), m(inutes), h(ours), or d(ays)
60+
; Default Unit: seconds
61+
; Default Value: 0
62+
;process_control_timeout = 0
63+
64+
; The maximum number of processes FPM will fork. This has been designed to control
65+
; the global number of processes when using dynamic PM within a lot of pools.
66+
; Use it with caution.
67+
; Note: A value of 0 indicates no limit
68+
; Default Value: 0
69+
; process.max = 128
70+
71+
; Specify the nice(2) priority to apply to the master process (only if set)
72+
; The value can vary from -19 (highest priority) to 20 (lowest priority)
73+
; Note: - It will only work if the FPM master process is launched as root
74+
; - The pool process will inherit the master process priority
75+
; unless specified otherwise
76+
; Default Value: no set
77+
; process.priority = -19
78+
79+
; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging.
80+
; Default Value: yes
81+
;daemonize = yes
82+
83+
; Set open file descriptor rlimit for the master process.
84+
; Default Value: system defined value
85+
;rlimit_files = 1024
86+
87+
; Set max core size rlimit for the master process.
88+
; Possible Values: 'unlimited' or an integer greater or equal to 0
89+
; Default Value: system defined value
90+
;rlimit_core = 0
91+
92+
; Specify the event mechanism FPM will use. The following is available:
93+
; - select (any POSIX os)
94+
; - poll (any POSIX os)
95+
; - epoll (linux >= 2.5.44)
96+
; - kqueue (FreeBSD >= 4.1, OpenBSD >= 2.9, NetBSD >= 2.0)
97+
; - /dev/poll (Solaris >= 7)
98+
; - port (Solaris >= 10)
99+
; Default Value: not set (auto detection)
100+
;events.mechanism = epoll
101+
102+
; When FPM is built with systemd integration, specify the interval,
103+
; in seconds, between health report notification to systemd.
104+
; Set to 0 to disable.
105+
; Available Units: s(econds), m(inutes), h(ours)
106+
; Default Unit: seconds
107+
; Default value: 10
108+
;systemd_interval = 10
109+
110+
;;;;;;;;;;;;;;;;;;;;
111+
; Pool Definitions ;
112+
;;;;;;;;;;;;;;;;;;;;
113+
114+
; Multiple pools of child processes may be started with different listening
115+
; ports and different management options. The name of the pool will be
116+
; used in logs and stats. There is no limitation on the number of pools which
117+
; FPM can handle. Your system will tell you anyway :)
118+
119+
; Include one or more files. If glob(3) exists, it is used to include a bunch of
120+
; files from a glob(3) pattern. This directive can be used everywhere in the
121+
; file.
122+
; Relative path can also be used. They will be prefixed by:
123+
; - the global prefix if it's been set (-p argument)
124+
; - /usr otherwise
125+
include=testData/php-fpm.d/*.conf

0 commit comments

Comments
 (0)