Wednesday, December 9, 2009

Space Alllocation Web Script for EMC LUNs

This is a JQuery / JQuery UI script I put together to calculate how much space to add to a filesystem based on percentage used.

Here is the HTML form

1:  <div id="formBox"> 
2: <form id="calcQuery" >
3: <fieldset>
4: <legend>File System Space Calculator</legend>
5: <p>
6: <label for="curr_alloc">Current Space Allocation:</label>
7: <br />
8: <input type="text" size="5" name="curr_alloc" id="curr_alloc" />
9: &nbsp;KB <input type="radio" name="curr_unit" value="KB" />
10: &nbsp;MB <input type="radio" name="curr_unit" value="MB" />
11: &nbsp;GB <input type="radio" name="curr_unit" value="GB" checked/>
12: &nbsp;TB <input type="radio" name="curr_unit" value="TB" />
13: </p>
14: <p>
15: <label for="curr_percent">Current Usage Percentage:</label>
16: <br />
17: <input type="text" size="5" name="curr_percent" id="curr_percent" />
18: </p>
19: <p>
20: <label for="desired_percent">Desired Usage Percentage:</label>
21: <br />
22: <input type="text" size="5" name="desired_percent" id="desired_percent" />
23: </p>
24: <br />
25: <p>
26: <!-- input type="submit" value="calculate"/ --></p>
27: </fieldset>
28: </form>
29: <div id="calcBox"> </div>
30: </div>


here is the javascript for that pops up a dialog with the above form

1:  function clearForm(form) 
2: {
3: $(":input", form).each(function()
4: {
5: var type = this.type;
6: var tag = this.tagName.toLowerCase();
7: if (type == 'text')
8: this.value = "";
9: else if (type == 'radio')
10: $('input[name=curr_unit][value=GB]').attr('checked', true);
11: });
12: };
13: // form popup
14: $(document).ready(function()
15: {
16: $("#formBox").dialog({
17: bgiframe: true,
18: autoOpen: false,
19: height: 600,
20: width: 400,
21: modal: false,
22: closeOnEscape: true,
23: close: function()
24: {
25: $("#calcBox").hide();
26: clearForm("#calcQuery");
27: },
28: title: "Calculator",
29: buttons: {
30: "Calculate": function() {
31: // form post
32: $.ajax({
33: type: "POST",
34: url: "calc.php",
35: data: $("#calcQuery").serialize(),
36: dataType: "html",
37: success: function(response)
38: {
39: $("#calcBox").html(response);
40: $("#calcBox").show();
41: clearForm("#calcQuery");
42: },
43: error: function
44: (xhr, ajaxOptions, thrownError)
45: {
46: alert(xhr.status);
47: alert(thrownError);
48: }
49: }).responseText;
50: // form post
51: }
52: }
53: });
54: $('#calcButton').click(function(){
55: $('#formBox').dialog('open');
56: return false;
57: });
58: });


and here is the PHP code that does the actual calculations ...

1:  <?php 
2: function KBtoGB($a)
3: {
4: /*
5: * Convert KiloBytes to GigaBytes
6: * and round up (if needed)
7: */
8: $b = ($a / 1024) / 1024;
9: return ceil($b);
10: }
11: function MBtoGB($a)
12: {
13: /*
14: * Convert MegaBytes to GigaBytes
15: * and round up (if needed)
16: */
17: $b = $a / 1024;
18: return ceil($b);
19: }
20: function TBtoGB($a)
21: {
22: /*
23: * Convert TeraBytes to GigaBytes
24: * and round up (if needed)
25: */
26: $b = $a * 1024;
27: return ceil($b);
28: }
29: function altUnit($fmeasure, $umeasure)
30: {
31: /*
32: * This function gives
33: * alternative file system size units of measure
34: * $fmeasure is the filesystem size
35: * $umeasure is the unit of measure used
36: *
37: * round($somenum, 2) rounds to the nearest two decimal places
38: *
39: */
40: switch ($umeasure)
41: {
42: case "KB":
43: if ($fmeasure >= 1024)
44: {
45: $h = $fmeasure /1024;
46: $i = " or ".round($h, 2)."MB";
47: if ($h >= 1024)
48: {
49: $j = $h /1024;
50: $i .= " or ".round($j, 2)."GB\n";
51: if ($j >= 1024)
52: {
53: $k = $j /1024;
54: $i .= " or ".round($k, 2)."TB\n";
55: return $i;
56: }
57: return $i;
58: }
59: return $i;
60: }
61: break;
62: case "MB":
63: if ($fmeasure >= 1024)
64: {
65: $h = $fmeasure /1024;
66: $i = " or ".round($h, 2)."GB";
67: if ($h >= 1024)
68: {
69: $j = $h /1024;
70: $i .= " or ".round($j, 2)."TB\n";
71: return $i;
72: }
73: return $i;
74: }
75: break;
76: case "GB":
77: if ($fmeasure >= 1024)
78: {
79: $h = $fmeasure /1024;
80: $i = " or ".round($h, 2)."TB";
81: return $i;
82: }
83: break;
84: case "TB":
85: $h = $fmeasure * 1024;
86: if ($h >= 1024)
87: {
88: $i = " or ".round($h, 2)."GB";
89: return $i;
90: }
91: break;
92: }
93: }
94: function calcLun($lsize, $fsize)
95: {
96: /*
97: * Calculate the number of LUNs needed
98: * according to the file system size
99: * the final number is then rounded up (if needed)
100: *
101: * lsize = size of LUN
102: * fsisze = filesystem size
103: * lnum = number of luns
104: */
105: $lnum = $fsize / $lsize;
106: return ceil($lnum);
107: }
108: function calcSpace($cur_alloc, $unit, $cur_percent, $des_percent)
109: {
110: // take the raw numbers and do the raw math
111: $a = (($cur_alloc * $cur_percent) / $des_percent) - $cur_alloc;
112: /*
113: * we do different stuff
114: * depending on what
115: * unit of measure was clicked in the form
116: *
117: * the KBtoGB, MBtoGB, TBtoGB and calcLuns functions
118: * are used below
119: *
120: * In case you don't know the ceil() function
121: * is a PHP built in function
122: * it takes a decimal number and rounds up
123: */
124: switch ($unit)
125: {
126: case "KB":
127: $b = KBtoGB($a);
128: $c = calcLun(33,$b);
129: $d = calcLun(72,$b);
130: $e = '(('.ceil($a).$unit.' / 1024) / 1024) / 33 = '.$c.' LUNs Required';
131: $f = '(('.ceil($a).$unit.' / 1024) / 1024) / 72 = '.$d.' LUNs Required';
132: break;
133: case "MB":
134: $b = MBtoGB($a);
135: $c = calcLun(33,$b);
136: $d = calcLun(72,$b);
137: $e = '( '.ceil($a).$unit.' / 1024 ) / 33 = '.$c.' LUNs Required';
138: $f = '( '.ceil($a).$unit.' / 1024 ) / 72 = '.$d.' LUNs Required';
139: break;
140: case "GB":
141: $c = calcLun(33,$a);
142: $d = calcLun(72,$a);
143: $e = ceil($a).' / 33 = '.$c.' LUNs Required';
144: $f = ceil($a).' / 72 = '.$d.' LUNs Required';
145: break;
146: case "TB":
147: $b = TBtoGB($a);
148: $c = calcLun(33,$b);
149: $d = calcLun(72,$b);
150: $e = '('.ceil($a).$unit.' * 1024) / 33 = '.$c.' LUNs Required';
151: $f = '('.ceil($a).$unit.' * 1024) / 72 = '.$d.' LUNs Required';
152: break;
153: }
154: $g = '(('.$cur_alloc.$unit.' * '.$cur_percent.'%) / '.$des_percent.'%) - '.$cur_alloc.$unit;
155: $g .= ' = '.$a.$unit." which rounds up to ".ceil($a).$unit;
156: //call the altUnit function
157: $i = altUnit($a,$unit);
158: /*
159: * if there are alternative Units to print out
160: * example: 1024 GB or 1 TB (TB is the alternative unit)
161: */
162: if ($i)
163: {
164: $z = "<br /><p>you need about ".ceil($a).$unit.'&nbsp;'.$i."</p>";
165: }
166: else
167: {
168: $z = "<br /><p>you need about ".ceil($a).$unit."</p>";
169: }
170: //end of if
171: $z .= "<br /><p>That would be :<br />";
172: $z .= "\t".ceil($c)." 33GB LUNs <br />";
173: $z .= "&nbsp;or<br />\n";
174: $z .= "\t".ceil($d)." 72GB LUNs <br />";
175: $z .= "</p>";
176: $z .= "<br /><hr /><br />\n";
177: $z .= $g.'<br /><br />&nbsp; For 33GB LUNs '.$e.'<br />&nbsp; For 72GB LUNs '.$f."\n";
178: $z .= '<br /><br /><table border="0"><tr style="border-bottom: solid 2px #000;>';
179: $z .= '<td rowspan="2" valign="center" align="center">Formula: </td>&nbsp;';
180: $z .= '<th style="border-bottom: solid 2px #000; valign="bottom" align="center">Current Allocation x Current Percentage Used</th>';
181: $z .= '<th valign="center" align="center" rowspan="2">&nbsp; - Current Allocation</tr>';
182: $z .= '<tr><td>&nbsp;</td><th>Desired Percentage</th><td>&nbsp;</td></tr></table>';
183: print "$z";
184: }
185: if ($_POST['curr_alloc'] and $_POST['curr_unit'] and $_POST['curr_percent'] and $_POST['desired_percent'])
186: {
187: calcSpace($_POST['curr_alloc'], $_POST['curr_unit'], $_POST['curr_percent'], $_POST['desired_percent']);
188: }
189: else
190: {
191: print '<h1 style="font-weight: bold; color: #A22903;">Something is missing in the form</h1>';print "\n";
192: }
193: ?>

No comments: