1#!/usr/local/bin/php
2<?php
3/**
4 * Drupal Page Speed Test
5 * Munin plugin to time a page speed return from the server
6 *
7 * It's required to define a container entry for this plugin in your
8 * /etc/munin/plugin-conf.d/munin-node (or a separate and dedicated file).
9 *
10 * @example Example entry for configuration:
11 * [drupal*]
12 * env.url_login http://www.example.com
13 *
14 * // Username and password should be set using key=value where key is the form input name and value is its value
15 * env.url_login_user signInName=admin
16 * env.url_login_pass password=admin
17 *
18 * // You may specify other key=value fields which may be required when submitting the form's POST action to perform the login
19 * env.url_login_post_fields target=http://www.example.com
20 *
21 * // Each page to test should be specified in the format of <label>=<url> where label is alphanumeric with no spaces and url is the page URL to test
22 * // You may specify multiple pages to time their page load by separating them with commas ,
23 * env.urls label_page1=http://www.example.com/page1.php,label_page2=http://www.example.com/page2.php
24 * env.cookie_file /tmp/cookie.txt
25 *
26 * @author Liran Tal <liran.tal@hp.com>
27 * @version 1.1 2013
28 *
29 */
30
31
32/**
33 * Environment variabels are set by munin's configuration
34 * @see /etc/munin/plugin-conf.d/munin-node
35 */
36$graph_period = getenv('graph_period');
37
38$settings['url_login'] = getenv('url_login');
39$settings['url_login_user'] = getenv('url_login_user');
40$settings['url_login_pass'] = getenv('url_login_pass');
41$settings['url_login_post_fields'] = getenv('url_login_post_fields');
42$settings['urls'] = getenv('urls');
43
44$urls_tmp = explode(',', $settings['urls']);
45$settings['test_pages'] = array();
46foreach ($urls_tmp as $url) {
47  $url_info = explode('=', $url);
48  if (!empty($url_info[0]) && !empty($url_info[1]))
49    $settings['test_pages'][$url_info[0]] = $url_info[1];
50}
51
52$cookie = getenv('cookie_file');
53if (empty($cookie))
54  $cookie = '/tmp/cookie.txt';
55
56$debug = FALSE;
57
58
59if(count($argv) == 2 && $argv[1] == 'autoconf') {
60  echo "yes\n";
61  exit(0);
62}
63
64if (count($argv) === 2 && $argv[1] === 'config') {
65  echo "graph_title Drupal Page Speed Test\n";
66  echo "graph_args --base 1000 --lower-limit 0\n";
67  echo "graph_vlabel Page Load Time / ${graph_period}\n";
68  echo "graph_category cms\n";
69  echo "graph_scale no\n";
70  echo "graph_info Displays the time it took for several pages to load\n";
71
72  foreach ($settings['test_pages'] as $label => $value) {
73    echo "$label.label $label\n";
74    echo "$label.type GAUGE\n";
75    echo "$label.min 0\n";
76  }
77
78  if (!empty($settings['url_login'])) {
79    echo "login.label login\n";
80    echo "login.type GAUGE\n";
81    echo "login.min 0\n";
82  }
83
84  exit(0);
85}
86
87
88// General curl settings
89$ch = curl_init();
90curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
91
92curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
93curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
94curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
95curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
96curl_setopt($ch, CURLOPT_TIMEOUT, 60);
97curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
98
99curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
100curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
101
102if (!empty($settings['url_login'])) {
103  $time_login = perform_login();
104  echo "login.value $time_login\n";
105}
106
107foreach ($settings['test_pages'] as $label => $value) {
108  $page_time = perform_get_page($value);
109  echo "$label.value $page_time\n";
110}
111
112curl_close($ch);
113
114
115
116
117function perform_get_page($url) {
118
119  global $ch;
120  global $settings;
121  global $debug;
122
123  $options['url'] = $url;
124
125  if ($debug)
126    print 'Getting: ' . $options['url'] . PHP_EOL;
127
128  curl_setopt($ch, CURLOPT_URL, $options['url']);
129
130  $start = microtime(true);
131  $result = curl_exec($ch);
132  $end = microtime(true);
133
134  if ($debug)
135    print $result;
136
137  $time = $end - $start;
138
139  if ($debug)
140    print $time . PHP_EOL;
141
142  return $time;
143
144}
145
146
147function perform_login() {
148
149  global $ch;
150  global $debug;
151  global $settings;
152
153  // define post fields
154  $user_info = explode('=', $settings['url_login_user']);
155  $pass_info = explode('=', $settings['url_login_pass']);
156
157  $tmp = explode(',', $settings['url_login_post_fields']);
158  $post_fields_extra = array();
159  foreach ($tmp as $tmp_post_field) {
160    $post_field_info = explode('=', $tmp_post_field);
161    if (!empty($post_field_info[0]) && !empty($post_field_info[1]))
162      $post_fields_extra[$post_field_info[0]] = $post_field_info[1];
163  }
164
165  // Add the username and password info
166  $post_fields_extra[$user_info[0]] = $user_info[1];
167  $post_fields_extra[$pass_info[0]] = $pass_info[1];
168
169  $return_url = "";
170
171  $post_fields = $post_fields_extra;
172
173  $options['url'] = $settings['url_login'];
174  $options['post_fields'] = http_build_query($post_fields);
175
176  if ($debug)
177    print 'Getting: ' . $options['url'] . PHP_EOL;
178
179  // if post fields provided, set the post fields data
180  if (isset($options['post_fields']))
181  {
182    curl_setopt($ch, CURLOPT_POSTFIELDS, $options['post_fields']);
183    curl_setopt($ch, CURLOPT_POST, TRUE);
184  }
185
186  curl_setopt($ch, CURLOPT_URL, $options['url']);
187
188  $start = microtime(true);
189  $result = curl_exec($ch);
190  $end = microtime(true);
191
192  if ($debug)
193    print $result;
194
195  $time = $end - $start;
196
197  if ($debug)
198    print $time . PHP_EOL;
199
200  return $time;
201
202}
203