-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchimedes.unsafe.inc
98 lines (83 loc) · 2.61 KB
/
archimedes.unsafe.inc
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
<?php
/**
* @file Archimedes collection methods that maybe unsafe to run
* at regular cron intervals.
*/
class ArchimedesDrupalUnsafeCollection {
/**
* The collection owl Archimedes Class.
*/
protected $archimedes;
/**
* Initialize class. Associate archimedes class.
*/
public function __construct(Archimedes $archimedes) {
$this->archimedes = $archimedes;
}
/**
* Collect hostname information from the server running this Drupal install.
*/
public function servername() {
$hostname = unix_exec("hostname -f");
$values = array();
$values[] = archimedes_value($hostname,'nodereference')
->addNode(array('title' => $hostname, 'type' => 'host'));
$this->archimedes
->createField('field_webserver', $values)
->invokeFacet();
return $this;
}
/**
* Collect database storage metrics.
*/
public function database_size() {
$db_type = $databases['default']['default']['driver'];
switch ($db_type) {
case 'pgsql':
$database_name = (string) $this->archimedes->getField('field_dbname');
if (empty($database_name)) {
throw Exception("No database name available to obtain PostgreSQL database size.");
}
$this->archimedes
->createField('field_db_size', db_query("SELECT pg_database_size('" . $database_name . "')")->fetchField());
break;
case 'mysql':
case 'mysqli':
$rows = db_query('SHOW TABLE STATUS');
$size = 0;
foreach ($rows as $row) {
$size += ($row->data_length + $row->index_length);
}
$this->archimedes
->createField('field_db_size', $size);
break;
}
// Fail silently for other database types.
return $this;
}
/**
* Obtain storage usage data.
*/
public function storage_usage() {
$dataSize = preg_split('/[\s]+/', $this->unix_exec("du -bsL " . drupal_realpath('public://')));
$rootSize = preg_split('/[\s]+/', $this->unix_exec("du -bsL " . DRUPAL_ROOT . base_path()));
$this->archimedes->createField('field_sitedata', $dataSize[0]);
$this->archimedes->createField('field_sitesize', $rootSize[0] - $dataSize[0]);
return $this;
}
/**
* Execute a shell command.
*
* Abstracted from Drush.
*/
protected function unix_exec($cmd) {
$args = func_get_args();
//do not change the command itself, just the parameters.
for ($x = 1; $x < sizeof($args); $x++) {
$args[$x] = escapeshellarg($args[$x]);
}
$command = call_user_func_array('sprintf', $args);
exec($command . ' 2>&1', $output, $result);
return $output[0];
}
}