]> git.openstreetmap.org Git - chef.git/blob - cookbooks/squid/recipes/default.rb
Add squid exporter
[chef.git] / cookbooks / squid / recipes / default.rb
1 #
2 # Cookbook:: squid
3 # Recipe:: default
4 #
5 # Copyright:: 2011, OpenStreetMap Foundation
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     https://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 include_recipe "apt"
21 include_recipe "munin"
22 include_recipe "prometheus"
23
24 if node[:squid][:version] >= 3
25   apt_package "squid" do
26     action :unlock
27   end
28
29   apt_package "squid-common" do
30     action :unlock
31   end
32
33   apt_package "squid" do
34     action :purge
35     only_if "dpkg-query -W squid | fgrep -q 2."
36   end
37
38   apt_package "squid-common" do
39     action :purge
40     only_if "dpkg-query -W squid-common | fgrep -q 2."
41   end
42
43   file "/store/squid/coss-01" do
44     action :delete
45     backup false
46   end
47
48   package "squidclient" do
49     action :upgrade
50   end
51 end
52
53 package "squid"
54 package "squidclient"
55
56 template "/etc/squid/squid.conf" do
57   source "squid.conf.erb"
58   owner "root"
59   group "root"
60   mode "644"
61 end
62
63 directory "/etc/squid/squid.conf.d" do
64   owner "root"
65   group "root"
66   mode "755"
67 end
68
69 Array(node[:squid][:cache_dir]).each do |cache_dir|
70   if cache_dir =~ /^coss (\S+) /
71     cache_dir = File.dirname(Regexp.last_match(1))
72   elsif cache_dir =~ /^\S+ (\S+) /
73     cache_dir = Regexp.last_match(1)
74   end
75
76   directory cache_dir do
77     owner "proxy"
78     group "proxy"
79     mode "750"
80     recursive true
81     notifies :restart, "service[squid]"
82   end
83 end
84
85 systemd_tmpfile "/var/run/squid" do
86   type "d"
87   owner "proxy"
88   group "proxy"
89   mode "0755"
90 end
91
92 address_families = %w[AF_UNIX AF_INET AF_INET6]
93
94 file "/etc/systemd/system/squid.service" do
95   action :delete
96 end
97
98 file "/etc/logrotate.d/squid.dpkg-dist" do
99   action :delete
100 end
101
102 squid_service_exec = if node[:lsb][:release].to_f < 20.04
103                        "/usr/sbin/squid -YC"
104                      else
105                        "/usr/sbin/squid --foreground -YC"
106                      end
107
108 systemd_service "squid" do
109   dropin "chef"
110   limit_nofile 98304
111   private_tmp true
112   private_devices node[:squid][:private_devices]
113   protect_system "full"
114   protect_home true
115   restrict_address_families address_families
116   restart "always"
117   exec_start "#{squid_service_exec}"
118 end
119
120 service "squid" do
121   action :enable
122   subscribes :restart, "systemd_service[squid]"
123   subscribes :restart, "template[/etc/squid/squid.conf]"
124   subscribes :reload, "template[/etc/resolv.conf]"
125 end
126
127 notify_group "squid-start" do
128   action :run
129   notifies :start, "service[squid]"
130 end
131
132 service "squid-restart" do
133   service_name "squid"
134   action :restart
135   only_if do
136     IO.popen(["squidclient", "--host=127.0.0.1", "--port=3128", "mgr:counters"]) do |io|
137       io.each.grep(/^[a-z][a-z_.]+ = -[0-9]+$/).count.positive?
138     end
139   end
140 end
141
142 munin_plugin "squid_cache"
143 munin_plugin "squid_times"
144 munin_plugin "squid_icp"
145 munin_plugin "squid_objectsize"
146 munin_plugin "squid_requests"
147 munin_plugin "squid_traffic"
148
149 munin_plugin "squid_delay_pools" do
150   action :delete
151 end
152
153 munin_plugin "squid_delay_pools_noreferer" do
154   action :delete
155 end
156
157 prometheus_exporter "squid" do
158   port 9301
159   listen_switch "listen"
160 end