├── LICENSE
├── README.md
├── cron.sh
├── interpretcron.sh
├── php-fpmpal.01.sh
├── php-fpmpal.py
└── php-fpmpal.sh
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # php-fpmpal
2 | Bash script script that makes recommendations on max_children for each PHP-FPM pool on a server.
3 |
4 | 1) What the main script (php-fpmpal.sh) does is:
5 | * identify all PHP-FPM pools on a server and the average memory usage
6 | per process for each pool
7 | * classify how much of the overall PHP-FPM memory each pool uses (e.g.
8 | pool one uses about 60%, pool2 uses 25% and pool3 uses 15%)
9 | * works out how much memory is available on the server for PHP-FPM to
10 | use as a whole
11 | * works out how much of that available memory should be allocated to
12 | each pool
13 | * works out the max_children setting for each pool based on it's
14 | "allocated" memory and the average process size for the pool
15 |
16 | 2) Cron job (cron.sh):
17 | * will catch various PHP-FPM pool stats and keep a set number of copies
18 | * saves stats to "/var/log/php-fpmpal/" by default
19 | * to setup cronjob create the file "/etc/cron.d/php-fpmpal-stats-capture" with content:
20 |
21 | SHELL=/bin/bash
22 |
23 | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
24 |
25 | */10 * * * * root /bin/bash /path/to/cron.sh
26 |
27 |
28 |
29 |
30 | 3) Cron job interpreter (interpretcron.sh):
31 | * can be used to print out various types of reports using stats captured in PHP-FPM cronjob
32 | * run "./interpretcron.sh --help" to get help on usage:
33 |
34 | Usage: interpretcron.sh [OPTION]
35 |
36 | Arguments:
37 |
38 | -ms For each logfile in /var/log/php-fpmpal show the total memory usage per PHP-FPM pool
39 |
40 | -msl [FILENAME] Show the total memory usage per PHP-FPM pool for this logfile
41 |
42 |
--------------------------------------------------------------------------------
/cron.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # Logrotate
4 | copies_to_keep=144
5 |
6 | # Log location and filename
7 | logdirectory="/var/log/php-fpmpal/"
8 | mkdir -p $logdirectory
9 |
10 | # Log file name
11 | logfile_name=$logdirectory`date "+%Y.%m.%d_%H:%M:%S"`
12 |
13 | # Create new log file
14 | touch $logfile_name
15 |
16 | # Put timestamp at top of logfile
17 | echo `date "+%Y.%m.%d_%H:%M:%S"` >> $logfile_name
18 |
19 | # Create a list of PHP-FPM pools
20 | IFS=$'\n' list_of_pools=($(ps aux | grep "php-fpm" | grep -v ^root | grep -v grep | awk -F "pool " '{print $2}' | sort | uniq | sed -e 's/ //g'))
21 |
22 | # Get total server memory
23 | total_server_memory=`cat /proc/meminfo | grep MemTotal | awk '{print $2}'`
24 |
25 | # Initialise total PHP-FPM memory usage
26 | total_phpfpm_mem_usage=0
27 |
28 | # Get the number of pools
29 | let no_of_pools=(${#list_of_pools[@]}-1)
30 |
31 | # Set the php-fpm process name
32 | php-fpm -v 2&>1 /dev/null
33 | if [ $? == 0 ]; then
34 | pool_directory="/etc/php-fpm.d/*.conf"
35 | fpm_type="php-fpm"
36 | fi
37 |
38 | php5-fpm -v 2&>1 /dev/null
39 | if [ $? == 0 ]; then
40 | pool_directory="/etc/php5/fpm/pool.d/*.conf"
41 | fpm_type="php5-fpm"
42 | fi
43 |
44 | # For each pool
45 | for ((i=0; i<=no_of_pools; i++))
46 | do
47 | # Get list of PIDs
48 | if [ $fpm_type == "php-fpm" ]; then
49 | IFS=$'\n' list_of_pids=($(ps aux | grep "php-fpm" | grep -v ^root | grep -v grep | grep "pool ${list_of_pools[$i]}$" | awk '{print $2}'))
50 | elif [ $fpm_type == "php5-fpm" ]; then ### Ubuntu/PHP5-FPM nuance in ps output
51 | IFS=$'\n' list_of_pids=($(ps aux | grep "php-fpm" | grep -v ^root | grep -v grep | grep "pool ${list_of_pools[$i]} " | awk '{print $2}'))
52 | fi
53 |
54 | total_pool_mem_usage[$i]=0
55 |
56 | # For every PID
57 | for ((a=0; a<=no_of_pids; a++))
58 | do
59 | # Get PID memory usage and add it to pool's total memory usage
60 | let total_pool_mem_usage[$i]+=`/usr/bin/pmap -d ${list_of_pids[$a]} | grep "writeable/private" | awk '{ print $4 }' | sed -e s/K//`
61 | done
62 | # Add total pool memory usage to total PHP-FPM memory usage
63 | let total_phpfpm_mem_usage+=${total_pool_mem_usage[$i]}
64 | # Calculate average process size for this pool
65 | pool_ave_process_size[$i]=`echo "${total_pool_mem_usage[$i]} / ${#list_of_pids[@]}" | bc`
66 | done
67 |
68 | # Get the number of PIDs
69 | no_of_pids=(${#list_of_pids[@]}-1)
70 |
71 |
72 | ### Write pool, PID and PID memory usage to logfile
73 | # For every pool
74 | for ((p=0; p<=no_of_pools; p++))
75 | do
76 | # Get the list of PIDs
77 | if [ $fpm_type == "php-fpm" ]; then
78 | IFS=$'\n' list_of_pids=($(ps aux | grep "php-fpm" | grep -v ^root | grep -v grep | grep "pool ${list_of_pools[$p]}$" | awk '{print $2}'))
79 | elif [ $fpm_type == "php5-fpm" ]; then ### Ubuntu/PHP5-FPM nuance in ps output
80 | IFS=$'\n' list_of_pids=($(ps aux | grep "php-fpm" | grep -v ^root | grep -v grep | grep "pool ${list_of_pools[$p]} " | awk '{print $2}'))
81 | fi
82 | # Set the number of PIDs
83 | no_of_pids=(${#list_of_pids[@]}-1)
84 | # For every PID
85 | for ((i=0; i<=no_of_pids; i++ ))
86 | do
87 | # Get PID memory usage and write to logfile
88 | echo ": ${list_of_pools[$p]} ${list_of_pids[$i]} `/usr/bin/pmap -d ${list_of_pids[$i]} | grep "writeable/private" | awk '{ print $4 }' | sed -e s/K//`" >> $logfile_name
89 | #grep "\[${list_of_pools[$p]}\]" ${list_of_includes[$i]} > /dev/null
90 | #if [ $? == 0 ]; then
91 | # pool_config_file[$p]="${list_of_includes[$i]}"
92 | #fi
93 | done
94 | done
95 |
96 |
97 | ### Write memory usage for different processes to logfile
98 | echo "Total server memory in KB: $total_server_memory" >> $logfile_name
99 |
100 | # Apache
101 | apache_process_name=`apachectl -V 2>&1 | grep PID | awk -F"run/" '{print $2}' | cut -d. -f1`
102 | ps aux | grep -v ^root | egrep $apache_process_name > /dev/null
103 | if [ $? == 0 ]; then
104 | IFS=$'\n' list_of_apache_pids=($(ps aux | grep $apache_process_name | grep -v ^root | grep -v grep | awk '{print $2}'))
105 | total_apache_pool_mem_usage=0
106 | for a in "${list_of_apache_pids[@]}"
107 | do
108 | let total_apache_pool_mem_usage+=`/usr/bin/pmap -d $a | grep "writeable/private" | awk '{ print $4 }' | sed -e s/K//`
109 | done
110 | else
111 | total_apache_pool_mem_usage=0
112 | fi
113 | echo "Total Apache memory usage in KB: $total_apache_pool_mem_usage" >> $logfile_name
114 |
115 | # Varnish
116 | ps aux | grep -v ^root | grep varnish > /dev/null
117 | if [ $? == 0 ]; then
118 | varnish_perc=`ps aux | grep -v ^root | grep varnish | awk '{print $4}'`
119 | total_varnish_mem_usage=`echo "$total_server_memory / 100 * $varnish_perc" | bc`
120 | else
121 | total_varnish_mem_usage=0
122 | fi
123 | echo "Total Varnish memory usage in KB: $total_varnish_mem_usage" >> $logfile_name
124 |
125 | # MySQL
126 | ps aux | grep -v ^root | grep mysql > /dev/null
127 | if [ $? == 0 ]; then
128 | mysql_perc=`ps aux | grep -v ^root | grep mysql | awk '{print $4}'`
129 | total_mysql_pool_mem_usage=`echo "$total_server_memory / 100 * $mysql_perc" | bc`
130 | else
131 | total_mysql_pool_mem_usage=0
132 | fi
133 | echo "Total MySQL memory usage in KB: $total_mysql_pool_mem_usage" >> $logfile_name
134 |
135 | # PHP-FPM
136 | echo "Current total PHP-FPM memory usage in KB: $total_phpfpm_mem_usage" >> $logfile_name
137 |
138 | # Memory available to PHP-FPM
139 | total_phpfpm_allowed_memory=`echo "$total_server_memory - ($total_apache_pool_mem_usage + $total_mysql_pool_mem_usage + $total_varnish_mem_usage)" | bc`
140 | echo "Memory available to assign to PHP-FPM pools in KB: $total_phpfpm_allowed_memory" >> $logfile_name
141 |
142 |
143 | ###
144 | echo "Pool Memory_usage % Allowed_mem_usage Ave_process_size Recommended_max_children Current_max_children" >> $logfile_name
145 |
146 | for ((i=0; i<=no_of_pools; i++))
147 | do
148 | pool_perc_mem_use[$i]=`echo "scale=2; ${total_pool_mem_usage[$i]}*100/$total_phpfpm_mem_usage" | bc`
149 | pool_allowed_mem_use[$i]=`echo "$total_phpfpm_allowed_memory * ${pool_perc_mem_use[$i]} / 100" | bc`
150 | pool_allowed_max_children[$i]=`echo "${pool_allowed_mem_use[$i]} / ${pool_ave_process_size[$i]}" | bc`
151 | config_file_location=`grep "\[${list_of_pools[$i]}\]" $pool_directory | cut -d: -f1`
152 | #current_max_children_value=`grep "^pm.max_children" $config_file_location | cut -d= -f2 | sed -e 's/ //g'`
153 | echo "${list_of_pools[$i]} ${total_pool_mem_usage[$i]} ${pool_perc_mem_use[$i]} ${pool_allowed_mem_use[$i]} ${pool_ave_process_size[$i]} ${pool_allowed_max_children[$i]} $current_max_children_value" >> $logfile_name
154 | done
155 |
156 |
157 | ### Rotate out old logfiles
158 | for i in `diff <(ls -tr /var/log/php-fpmpal/ | tail -$copies_to_keep) <(ls -tr /var/log/php-fpmpal/) | awk '/^>.*/ {print $2}'`
159 | do
160 | rm -f $logdirectory$i
161 | done
162 |
--------------------------------------------------------------------------------
/interpretcron.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | cron_log_location="/var/log/php-fpmpal"
4 |
5 | function pool_summary_memory()
6 | {
7 | echo -e "\e[32m\e[1m==================================="
8 | echo -e "TOTAL MEMORY USAGE PER PHP-FPM POOL"
9 | echo -e "===================================\e[0m"
10 | echo
11 |
12 | for i in `ls -tr /var/log/php-fpmpal/`
13 | do
14 | echo -ne "\e[32m\e[1m===== "
15 | echo -ne $(head -1 /var/log/php-fpmpal/$i)
16 | echo -e " ====="
17 | echo -e "\e[36mPool \e[38;5;208mTotal size (MB) \e[33mNr. of processes\e[0m"
18 | #awk '/^:.*/ {sum4[$2] += $4} {count[$2]++}; END {for (id in sum4) {print "\033[36m-- " id " -- \033[38;5;208m", sum4[id]/1024 " \033[33m" count[id] } }' < /var/log/php-fpmpal/$i
19 | #awk '/^:.*/ {sum += $4}; END {print "\033[31m\033[1mTOTAL: " sum/1024 " MB" }' < /var/log/php-fpmpal/$i
20 | awk '/^:.*/ {sum4[$2] += $4} {count[$2]++}; END {for (id in sum4) {print "\033[36m-- " id " -- \033[38;5;208m", sum4[id]/1024 " \033[33m" count[id] } }' < $cron_log_location/$i
21 | awk '/^:.*/ {sum += $4}; END {print "\033[31m\033[1mTOTAL: " sum/1024 " MB" }' < $cron_log_location/$i
22 | echo
23 | done
24 | }
25 |
26 |
27 | function pool_summary_memory_single_file()
28 | {
29 | echo -ne "\e[32m\e[1m===== "
30 | echo -ne $(head -1 $1)
31 | echo -e " ====="
32 | echo -e "\e[36mPool \e[38;5;208mTotal size (MB) \e[33mNr. of processes\e[0m"
33 | awk '/^:.*/ {sum4[$2] += $4} {count[$2]++}; END {for (id in sum4) {print "\033[36m-- " id " -- \033[38;5;208m", sum4[id]/1024 " \033[33m" count[id] } }' < $1
34 | awk '/^:.*/ {sum += $4}; END {print "\033[31m\033[1mTOTAL: " sum/1024 " MB" }' < $1
35 | echo
36 | }
37 |
38 |
39 | function single_pool_summary_through_all_logfiles()
40 | {
41 | echo -e "\e[32m\e[1m==================================="
42 | echo -e "TOTAL MEMORY USAGE PER PHP-FPM POOL"
43 | echo -e "===================================\e[0m"
44 | echo
45 | echo -e "\e[32m\e[1mTimestamp \e[36mPool \e[38;5;208mTotal size (MB) \e[33mNr. of processes Percentage\e[0m"
46 |
47 | for i in `ls -tr /var/log/php-fpmpal/`
48 | do
49 | echo -ne "\e[32m\e[1m$(head -1 /var/log/php-fpmpal/$i)"
50 | total_pool_mem=`awk '/^:.*/ {sum += $4}; END {print sum/1024 }' < $cron_log_location/$i`
51 | # grep -v "^: $1 " $cron_log_location/$i | awk -v var="$total_pool_mem" '{sum4[$2] += $4} {count[$2]++}; END {for (id in sum4) {print " \033[36m" id "\033[38;5;208m ", sum4[id]/1024 " \033[33m" count[id], " " var/count[id]"%" } }'
52 | grep "^: $1 " $cron_log_location/$i | awk '{sum4[$2] += $4} {count[$2]++}; END {for (id in sum4) {print " \033[36m" id "\033[38;5;208m ", sum4[id]/1024 " \033[33m" count[id] } }'
53 | done
54 | }
55 |
56 |
57 | function usage()
58 | {
59 | echo
60 | echo "Usage: interpretcron.sh [OPTION]"
61 | echo
62 | echo "Arguments:"
63 | echo " -ms For each logfile in $cron_log_location show the total memory usage per PHP-FPM pool"
64 | echo " -msl [FILENAME] Show the total memory usage per PHP-FPM pool for this logfile"
65 | echo " -sp [POOL] Show one pool's usage through all logfiles"
66 | echo
67 | }
68 |
69 | if [ $# -eq 0 ]; then
70 | echo
71 | echo "No arguments provided"
72 | usage
73 | exit 1
74 | fi
75 |
76 | if [ $1 == "--help" ]; then
77 | usage
78 | elif [ $1 == "-ms" ]; then
79 | pool_summary_memory
80 | elif [ $1 == "-msl" ]; then
81 | pool_summary_memory_single_file $2
82 | elif [ $1 == "-sp" ]; then
83 | single_pool_summary_through_all_logfiles $2
84 | fi
85 | #echo $1
86 |
87 | echo -e "\e[0m"
88 |
89 | #for i in `ls -tr /var/log/php-fpmpal/`; do echo -n "===== "; echo -n $(head -1 /var/log/php-fpmpal/$i); echo " ====="; awk '/^:.*/ {sum4[$2] += $4}; END{for (id in sum4) {print id, sum4[id] } }' < /var/log/php-fpmpal/$i; done
90 |
--------------------------------------------------------------------------------
/php-fpmpal.01.sh:
--------------------------------------------------------------------------------
1 | #! /bin/bash
2 |
3 |
4 | #============================================================================================
5 | #
6 | # +---------------------------------------------------------------------+
7 | # | |
8 | # | Logo method |
9 | # | |
10 | # +---------------------------------------------------------------------+
11 | #
12 | function print_logo ()
13 | {
14 | echo
15 | echo -e "\e[38;5;81m ( ) ( ( ( * "
16 | echo -e "\e[38;5;51m\e[1m )\ ) ( /( )\ ) )\ ) )\ ) ( \` \e[33m\e[1m ( "
17 | echo -e "\e[38;5;45m(()/( )\())(()/( (()/( (()/( )\))( \e[38;5;214m ) )\ "
18 | echo -e "\e[38;5;33m /(_))((_)\ /(_)) /(_)) /(_))((_)()\ \e[38;5;208m \` ) ( /( ((_) "
19 | echo -e "\e[38;5;27m(_)) _((_)(_)) (_))_|(_)) (_()((_) \e[38;5;124m/(/( )(_)) _ "
20 | echo -e "\e[0m\e[38;5;21m| _ \ | || || _ \ ___ | |_ | _ \ | \/ |\e[38;5;88m\e[1m((_)_\ \e[38;5;88m((_)\e[0m\e[38;5;9m_ | | "
21 | echo -ne "\e[38;5;21m| _/ | __ || _/|___|| __| | _/ | |\/| |\e[38;5;9m| '_ \\"
22 | echo -e "\e[38;5;88m\e[1m)\e[0m\e[38;5;9m/ _\` || | "
23 | echo -e "\e[38;5;21m|_| |_||_||_| |_| |_| |_| |_|\e[38;5;9m| .__/ \__,_||_| "
24 | for i in {17..21} {21..17} {17..21} {21..17} {17..21} {21..17} {17..21} {21..17} {17..17} ; do echo -en "\e[38;5;${i}m=\e[0m" ; done
25 | echo -en "\e[38;5;9m |_| "
26 | for i in {17..21} {21..21} {21..21} {21..21} {20..16} ; do echo -en "\e[38;5;${i}m=\e[0m" ; done
27 | echo -e "\e[0m"
28 | }
29 | #
30 | # +---------------------------------------------------------------------+
31 | # | |
32 | # | /// END OF Logo method /// |
33 | # | |
34 | # +---------------------------------------------------------------------+
35 | #
36 | #============================================================================================
37 | #
38 | # +---------------------------------------------------------------------+
39 | # | |
40 | # | Pre-checks methods |
41 | # | |
42 | # +---------------------------------------------------------------------+
43 | #
44 | # +---------------------------------------------------------------------+
45 | # | Determine if PHP-FPM is installed and what the process name is |
46 | # +---------------------------------------------------------------------+
47 | phpfpm_installed=0 # flag - is PHP-FPM installed? ( 0=No 1=Yes )
48 | fpm_type='' # PHP-FPM process name ( php-fpm php5-fpm )
49 |
50 | function check_php-fpm_installed_and_processname ()
51 | {
52 | php-fpm -v 1> /dev/null 2>&1
53 | if [ $? == 0 ]; then
54 | phpfpm_installed=1
55 | fpm_type=`php-fpm -i 2>&1 | grep "SERVER\[\"_\"\]" | cut -d\/ -f4`
56 | fi
57 |
58 | php5-fpm -v 1> /dev/null 2>&1
59 | if [ $? == 0 ]; then
60 | phpfpm_installed=1
61 | fpm_type=`php5-fpm -i 2>&1 | grep "SERVER\[\"_\"\]" | cut -d\/ -f4`
62 | fi
63 |
64 | php-fpm7.2 -v 1> /dev/null 2>&1
65 | if [ $? == 0 ]; then
66 | phpfpm_installed=1
67 | fpm_type=`php-fpm7.2 -i 2>&1 | grep "SERVER\['_'\]" | cut -d\/ -f4`
68 | fi
69 |
70 |
71 | if [ $phpfpm_installed == 0 ]; then # Exit if PHP-FPM is not installed
72 | echo -e "\e[31m!!! PHP-FPM not detected. Exiting. !!!\e[0m"
73 | echo
74 | exit 1
75 | fi
76 | }
77 |
78 | # +---------------------------------------------------------------------+
79 | # | Determine if PHP-FPM is running |
80 | # +---------------------------------------------------------------------+
81 | function check_php-fpm_running ()
82 | {
83 | ps aux | grep "php-fpm" | grep -v grep | grep -v "php-fpmpal" 1> /dev/null 2>&1
84 | if [ $? == 1 ]; then
85 | echo -e "\e[31m!!! PHP-FPM is installed but not running. PHP-FPM should be started before running this script. Exiting. !!!\e[0m"
86 | echo
87 | exit 1
88 | fi
89 | }
90 |
91 | # +---------------------------------------------------------------------+
92 | # | Determine if BC is installed |
93 | # +---------------------------------------------------------------------+
94 | function check_bc_install ()
95 | {
96 | bc -v 1> /dev/null 2>&1
97 | if [ $? != 0 ]; then
98 | echo -e "\e[31m\"bc\" is not installed. This script depends on it. Exiting. \e[0m"
99 | echo
100 | exit 1
101 | fi
102 | }
103 | #
104 | # +---------------------------------------------------------------------+
105 | # | |
106 | # | /// END OF Pre-checks methods /// |
107 | # | |
108 | # +---------------------------------------------------------------------+
109 | #
110 | #============================================================================================
111 | #
112 | # +---------------------------------------------------------------------+
113 | # | |
114 | # | Variable initialisation and general info gathering |
115 | # | |
116 | # +---------------------------------------------------------------------+
117 | #
118 | function variable_initialisation ()
119 | {
120 | # Apache root and config file
121 | httpd_root=`apachectl -V | awk -F "\"" '/HTTPD_ROOT/ {print $2}'`
122 | httpd_config_file=`apachectl -V | awk -F "\"" '/SERVER_CONFIG_FILE/ {print $2}'`
123 | httpd_root_and_config_file=$httpd_root"/"$httpd_config_file
124 |
125 | # List of Apache Includes (only 1 level deep from main configuration file)
126 | httpd_main_include=`awk -v var="$httpd_root" '/^Include/ {print var "/" $2}' $httpd_root_and_config_file`
127 | IFS=$'\n' list_of_apache_includes=($(ls $httpd_main_include))
128 |
129 | ### Get list of all PHP-FPM pools from process list
130 | IFS=$'\n' list_of_pools=($(ps aux | grep "php-fpm" | grep -v ^root | grep -v grep | awk -F "pool " '{print $2}' | sort | uniq | sed -e 's/ //g'))
131 |
132 | ### Get total server memory
133 | total_server_memory=`cat /proc/meminfo | grep MemTotal | awk '{print $2}'`
134 |
135 | ### Initialise variable to tally up total current PHP-FPM memory usage
136 | total_phpfpm_mem_usage=0
137 |
138 | ### Initialise variable to tally up total potential PHP-FPM memory usage based on largest processes in pool
139 | total_phpfpm_mem_usage_largest_process=0
140 |
141 | ### Initialise variable to tally up total potential PHP-FPM memory usage based on average process size in pool
142 | total_phpfpm_mem_usage_average_process=0
143 |
144 | ### Set variable to the total number of PHP-FPM pools
145 | let no_of_pools=(${#list_of_pools[@]}-1)
146 |
147 | ### Get list of possible configuration files
148 | IFS=$'\n' list_of_includes=($(grep -i ^include `ps aux | egrep "php.*master" | grep -v grep | cut -d\( -f2 | cut -d\) -f1` | cut -d= -f2))
149 |
150 | ### Set total number of include files
151 | let no_of_includes=(${#list_of_includes[@]}-1)
152 |
153 | ### Match each pool to its configuration file
154 | function match_pool_to_config_file ()
155 | {
156 | for ((p=0; p<=no_of_pools; p++))
157 | do
158 | for ((i=0; i<=no_of_includes; i++ ))
159 | do
160 | grep "\[${list_of_pools[$p]}\]" ${list_of_includes[$i]} > /dev/null
161 | if [ $? == 0 ]; then
162 | pool_config_file[$p]="${list_of_includes[$i]}"
163 | fi
164 | done
165 | done
166 | }
167 | match_pool_to_config_file
168 | }
169 | #
170 | # +---------------------------------------------------------------------+
171 | # | |
172 | # | /// END OF Variable initialisation and general info gathering /// |
173 | # | |
174 | # +---------------------------------------------------------------------+
175 | #
176 | #============================================================================================
177 | #
178 | # +---------------------------------------------------------------------+
179 | # | |
180 | # | Print out PHP-FPM pool information |
181 | # | |
182 | # +---------------------------------------------------------------------+
183 | #
184 | function display_pool_information ()
185 | {
186 | echo
187 | echo -en "\e[38;5;22m=\e[38;5;28m=\e[38;5;34m=\e[38;5;40m=\e[38;5;46m="
188 | echo -en "\e[32m\e[1m List of PHP-FPM pools \e[0m"
189 | echo -e "\e[38;5;46m=\e[38;5;40m=\e[38;5;34m=\e[38;5;28m=\e[38;5;22m=\e[0m"
190 |
191 | ### For each pool
192 | for ((i=0; i<=no_of_pools; i++))
193 | do
194 | ### Print pool name and configuration file
195 | echo -en "\t\e[36m\e[1m--- "
196 | echo -en ${list_of_pools[$i]}
197 | echo -e " ---\e[0m"
198 |
199 | ### Create a list of process IDs that belong to this pool
200 | IFS=$'\n' list_of_pids=($(ps aux | grep "php-fpm" | grep -v ^root | grep -v grep | grep pool | awk '{print $2, $13}' | grep " ${list_of_pools[$i]}$" | awk '{print $1}'))
201 |
202 | ### Calculate the total memory usage for this pool
203 | total_pool_mem_usage[$i]=0
204 | ### For each process
205 | for a in "${list_of_pids[@]}"
206 | do
207 | ### Gather memory usage (using pmap) and add it to total_pool_mem_usage variable
208 | let total_pool_mem_usage[$i]+=`pmap -d $a | grep "writeable/private" | awk '{ print $4 }' | sed -e s/K//`
209 | done
210 | ### Print total memory usage for this pool
211 | echo -en "\t\tTotal memory used: \e[1m"
212 | echo -en `echo "scale =2; ${total_pool_mem_usage[$i]}" / 1024 | bc`; echo -en " MB\e[0m\t"
213 | ### Increase the total PHP-FPM memory usage with the total memory usage for this pool
214 | let total_phpfpm_mem_usage+=${total_pool_mem_usage[$i]}
215 |
216 | ### List total number of processes that belong to this pool
217 | echo -n "Processes: "
218 | echo -en "\e[1m${#list_of_pids[@]}\e[0m"
219 |
220 | ### Get the current max_children value
221 | echo -en " \e[2m(max_children: "
222 | current_max_children_value=`grep "^pm.max_children" ${pool_config_file[$i]} | cut -d= -f2 | sed -e 's/ //g' | tail -1`
223 | echo -en "$current_max_children_value)\e[0m\t"
224 |
225 | ### Calculate the average process memory usage for this pool
226 | echo -en "Ave. process: \e[1m"
227 | pool_ave_process_size[$i]=`echo "${total_pool_mem_usage[$i]} / ${#list_of_pids[@]}" | bc`
228 | echo -en `echo "scale =2; ${pool_ave_process_size[$i]}" / 1024 | bc`; echo -e " MB\e[0m"
229 |
230 | ### Total potential memory usage for pool based on average process size: max_children * average process size
231 | potential_mem_usage=`echo "$current_max_children_value * ${pool_ave_process_size[$i]}" | bc`
232 | let total_phpfpm_mem_usage_average_process+=$potential_mem_usage
233 | done
234 |
235 | }
236 | #
237 | # +---------------------------------------------------------------------+
238 | # | |
239 | # | /// END OF Print out PHP-FPM pool information /// |
240 | # | |
241 | # +---------------------------------------------------------------------+
242 | #
243 | #============================================================================================
244 | #
245 | # +---------------------------------------------------------------------+
246 | # | |
247 | # | Print out server memory information |
248 | # | |
249 | # +---------------------------------------------------------------------+
250 | #
251 | function display_server_memory_information ()
252 | {
253 | echo
254 | echo -en "\e[38;5;22m=\e[38;5;28m=\e[38;5;34m=\e[38;5;40m=\e[38;5;46m="
255 | echo -en "\e[32m\e[1m Server memory usage statistics \e[0m"
256 | echo -e "\e[38;5;46m=\e[38;5;40m=\e[38;5;34m=\e[38;5;28m=\e[38;5;22m=\e[0m"
257 |
258 | ### Total server memory
259 | echo -ne "\tTotal server memory: "
260 | echo -ne "\t\t\t\t\e[1m"
261 | echo -ne `echo "scale =2; $total_server_memory" / 1024 | bc`; echo -e " MB\e[0m"
262 |
263 | # Calculate total memory available to assign to PHP-FPM
264 | echo -en "\tMemory available to assign to PHP-FPM pools: "
265 |
266 | ### Total memory usage - (the sum of all other processes listed above)
267 | ### Take total free memory and add current PHP-FPM total memory usage
268 |
269 | # RHEL 7's free reports look different to CentOS6, Ubuntu 14 and Debian 8 so I have to 1) check whether this is RHEL/CentOS 7, and 2) if it is, use different formulas
270 | rhel7_check=0
271 | if [ -f /etc/redhat-release ]; then
272 | rhel7_check=`cat /etc/redhat-release | awk -F "release" '{print $2}' | awk '{print $1}' | cut -d. -f1` > /dev/null
273 | fi
274 |
275 | # Ubuntu 18.04's free report looks different so we use a different formula
276 | ubuntu1804_check=0
277 | if [ -f /etc/lsb-release ]; then
278 | ubuntu1804_check=`grep DISTRIB_ID /etc/lsb-release | cut -d= -f2`
279 | if [ $ubuntu1804_check == 'Ubuntu' ]; then
280 | ubuntu1804_check=`grep DISTRIB_RELEASE /etc/lsb-release | cut -d= -f2`
281 | if [ $ubuntu1804_check == '18.04' ]; then
282 | ubuntu1804_check=1
283 | else
284 | ubuntu1804_check=0
285 | fi
286 | else
287 | ubuntu1804_check=0
288 | fi
289 | fi
290 |
291 | # If this is RHEL 7 then use this formula
292 | if [ $rhel7_check == '7' ]; then
293 | total_phpfpm_allowed_memory=$(echo "`free -k | awk '/Mem/ {print $7}'` + $total_phpfpm_mem_usage" | bc)
294 |
295 | # If this is Ubuntu 18.04 then use this formula
296 | elif [ $ubuntu1804_check == '1' ]; then
297 | total_phpfpm_allowed_memory=$(echo "`free -k | awk '/Mem/ {print $7}'` + $total_phpfpm_mem_usage" | bc)
298 |
299 | # If this is not RHEL 7 or Ubuntu 18.04 use this formula
300 | else
301 | total_phpfpm_allowed_memory=$(echo "`free -k | awk '/buffers\/cache/ {print $4}'` + $total_phpfpm_mem_usage" | bc)
302 | fi
303 | echo -ne "\t\e[1m"
304 | echo -ne `echo "scale =2; $total_phpfpm_allowed_memory" / 1024 | bc`; echo -e " MB\e[0m"
305 | echo -e "\t\t\e[2m(total free memory + PHP-FPM's current memory usage)\e[0m"
306 | echo
307 |
308 | ### Print out total potential PHP-FPM usage based on average process size per pool
309 | echo -en "\tPotential PHP-FPM memory usage: "
310 | echo -en "\t\t\e[1m"
311 | echo -ne `echo "scale =2; $total_phpfpm_mem_usage_average_process" / 1024 | bc`; echo -en " MB\e[0m "
312 | echo -ne "\e[33m (`echo "scale=2; $total_phpfpm_mem_usage_average_process*100/$total_phpfpm_allowed_memory" | bc `"
313 | echo -n "%)"
314 |
315 | # Print warning if this is larger than allowed PHP-FPM memory usage
316 | if [ $total_phpfpm_mem_usage_average_process -gt $total_phpfpm_allowed_memory ]; then
317 | echo -e " \e[31m!!! THIS IS LARGER THAN THE ASSIGNED MEMORY USAGE !!!\e[0m"
318 | else
319 | echo -e " \e[32m...GOOD :-)\e[0m"
320 | fi
321 | echo -e "\t\t\e[2m(based on current ave. process and max_children)\e[0m"
322 | echo
323 | }
324 | #
325 | # +---------------------------------------------------------------------+
326 | # | |
327 | # | /// END OF Print out server memory information /// |
328 | # | |
329 | # +---------------------------------------------------------------------+
330 | #
331 | #============================================================================================
332 | #
333 | # +---------------------------------------------------------------------+
334 | # | |
335 | # | Calculate and display pool recommendations |
336 | # | |
337 | # +---------------------------------------------------------------------+
338 | #
339 | # +---------------------------------------------------------------------+
340 | # | Succinct pool recommendations |
341 | # +---------------------------------------------------------------------+
342 | function succinct_pool_recommendations ()
343 | {
344 | echo -en "\e[38;5;22m=\e[38;5;28m=\e[38;5;34m=\e[38;5;40m=\e[38;5;46m="
345 | echo -en "\e[32m\e[1m Recommendations per pool \e[0m"
346 | echo -e "\e[38;5;46m=\e[38;5;40m=\e[38;5;34m=\e[38;5;28m=\e[38;5;22m=\e[0m"
347 |
348 | ### For every PHP-FPM pool
349 | for ((i=0; i<=no_of_pools; i++))
350 | do
351 | ### Calculate percentage of memory that pool is using against total PHP-FPM memory usage
352 | pool_perc_mem_use[$i]=`echo "scale=2; ${total_pool_mem_usage[$i]}*100/$total_phpfpm_mem_usage" | bc`
353 |
354 | ### Take that percentage and divide it by the total memory that should be available for PHP-FPM to use to get the total "allowed" memory usage for this pool
355 | pool_allowed_mem_use[$i]=`echo "$total_phpfpm_allowed_memory * ${pool_perc_mem_use[$i]} / 100" | bc`
356 |
357 | ### Divide the average process size for this pool by the total "allowed" memory usage for this pool to get a recommended max_children value
358 | pool_allowed_max_children[$i]=`echo "${pool_allowed_mem_use[$i]} / ${pool_ave_process_size[$i]}" | bc`
359 |
360 | ### Get the current max_children value for this PHP-FPM pool
361 | current_max_children_value=`grep "^pm.max_children" ${pool_config_file[$i]} | cut -d= -f2 | sed -e 's/ //g' | tail -1`
362 |
363 | ### Print out all this information
364 | echo -e "\t\e[36m\e[1m--- ${list_of_pools[$i]} ---\e[0m"
365 | echo -ne "\t\tCurrent usage: \e[1m${pool_perc_mem_use[$i]}%\e[0m"
366 | echo -ne "\tRecommended max_children: \e[1m${pool_allowed_max_children[$i]}\e[0m"
367 | echo -ne "\e[2m (current: $current_max_children_value)\e[0m"
368 | echo -ne "\tConfig file: ${pool_config_file[$i]}"
369 | echo
370 | done
371 | echo
372 | }
373 |
374 | # +---------------------------------------------------------------------+
375 | # | Detailed pool recommendations |
376 | # +---------------------------------------------------------------------+
377 | function detailed_pool_recommendations ()
378 | {
379 | echo -en "\e[38;5;22m=\e[38;5;28m=\e[38;5;34m=\e[38;5;40m=\e[38;5;46m="
380 | echo -en "\e[32m\e[1m Recommendations per pool \e[0m"
381 | echo -e "\e[38;5;46m=\e[38;5;40m=\e[38;5;34m=\e[38;5;28m=\e[38;5;22m=\e[0m"
382 |
383 | ### For every PHP-FPM pool
384 | for ((i=0; i<=no_of_pools; i++))
385 | do
386 | ### Calculate percentage of memory that pool is using against total PHP-FPM memory usage
387 | pool_perc_mem_use[$i]=`echo "scale=2; ${total_pool_mem_usage[$i]}*100/$total_phpfpm_mem_usage" | bc`
388 |
389 | ### Take that percentage and divide it by the total memory that should be available for PHP-FPM to use to get the total "allowed" memory usage for this pool
390 | pool_allowed_mem_use[$i]=`echo "$total_phpfpm_allowed_memory * ${pool_perc_mem_use[$i]} / 100" | bc`
391 |
392 | ### Divide the average process size for this pool by the total "allowed" memory usage for this pool to get a recommended max_children value
393 | pool_allowed_max_children[$i]=`echo "${pool_allowed_mem_use[$i]} / ${pool_ave_process_size[$i]}" | bc`
394 |
395 | ### Get the current max_children value for this PHP-FPM pool
396 | current_max_children_value=`grep "^pm.max_children" ${pool_config_file[$i]} | cut -d= -f2 | sed -e 's/ //g' | tail -1`
397 |
398 | ### Print out all this information
399 | echo -e "\t\e[36m\e[1m--- ${list_of_pools[$i]} ---\e[0m"
400 | echo -ne "\t\tCurrent usage: \e[1m${pool_perc_mem_use[$i]}%\e[0m"
401 | echo -ne "\tRecommended max_children: \e[1m${pool_allowed_max_children[$i]}\e[0m"
402 | echo -ne "\e[2m (current: $current_max_children_value)\e[0m"
403 | echo -ne "\tConfig file: ${pool_config_file[$i]}"
404 | echo
405 | echo -ne "\t\tMemory usage: \e[1m"
406 | echo -ne `echo "scale =2; ${total_pool_mem_usage[$i]}" / 1024 | bc`; echo -ne " MB\e[0m"
407 | echo -ne "\tAllowed usage: \e[1m"
408 | echo -ne `echo "scale =2; ${pool_allowed_mem_use[$i]}" / 1024 | bc`; echo -ne " MB\e[0m"
409 | echo
410 | done
411 | echo
412 | }
413 | #
414 | # +---------------------------------------------------------------------+
415 | # | |
416 | # | /// END OF Calculate and display pool recommendations //// |
417 | # | |
418 | # +---------------------------------------------------------------------+
419 | #
420 | #============================================================================================
421 | #
422 | # +---------------------------------------------------------------------+
423 | # | |
424 | # | Other recommendations methods |
425 | # | |
426 | # +---------------------------------------------------------------------+
427 | #
428 | # +---------------------------------------------------------------------+
429 | # | Observations from the pool logfiles |
430 | # +---------------------------------------------------------------------+
431 | function logfile_observations ()
432 | {
433 | echo -en "\e[38;5;22m=\e[38;5;28m=\e[38;5;34m=\e[38;5;40m=\e[38;5;46m="
434 | echo -en "\e[32m\e[1m Observations from the pool logfiles \e[0m"
435 | echo -e "\e[38;5;46m=\e[38;5;40m=\e[38;5;34m=\e[38;5;28m=\e[38;5;22m=\e[0m"
436 |
437 | # Find PHP-FPM main configuration file
438 | main_phpfpm_config_file=`ps aux | grep "php-fpm: master process" | grep -v grep | awk -F 'process' '{print $2}' | sed -e 's/[() ]//g'`
439 |
440 | # Get error log location from main configuration file
441 | error_log_location=`grep "^error_log" $main_phpfpm_config_file | awk '{print $3}'`
442 |
443 | # Find out whether there were any max_children errors in the logs
444 | errors_in_logs=`zgrep "server reached pm.max_children" $error_log_location* | wc -l` > /dev/null
445 |
446 | # If there were max_children errors
447 | if [ $errors_in_logs != 0 ]; then
448 |
449 | # Print out consideration regarding this
450 | echo "\tFrom the PHP-FPM error logfiles ($error_log_location):"
451 | zgrep "\tserver reached pm.max_children" $error_log_location* | awk '{print $5, $10}' | sed -e 's/[(),]//g' | sed -e 's/\]//g' | sort | uniq -c | awk '{print " - pool \033[36m" $2 "\033[0m had reached its max_children value of " $3 " on " $1 " occasion(s)"}'
452 | echo
453 | echo "\tFor these pools you may want to compare the recommended max_children value to this information, and decide whether the recommended value would be high enough to prevent max_children from being hit in future."
454 |
455 | # Else print that there are no further recommendations
456 | else
457 | echo -e "\tNo other recommendations/observations at this stage."
458 | fi
459 | }
460 |
461 | # +---------------------------------------------------------------------+
462 | # | General information about this script's usage |
463 | # +---------------------------------------------------------------------+
464 | function general_script_info ()
465 | {
466 | echo
467 | echo -en "\e[38;5;22m=\e[38;5;28m=\e[38;5;34m=\e[38;5;40m=\e[38;5;46m="
468 | echo -en "\e[32m\e[1m General information about this script's usage \e[0m"
469 | echo -e "\e[38;5;46m=\e[38;5;40m=\e[38;5;34m=\e[38;5;28m=\e[38;5;22m=\e[0m"
470 | echo -e "\tNote: It is not ideal to run PHP-FPMpal shortly after restarting PHP-FPM or your webservices. This is because PHP-FPMpal makes recommendations based on the average pool process size, and if PHP-FPM was restarted a short while ago then the likelihood is high that there won't have been many requests made to the sites since the restart, and metrics will be skewed and not show a normalised average."
471 | echo -e "\tIt is also worth noting that if you've recently restarted any services that normally use up a large amount of memory then you probably want to wait a while before running PHP-FPMpal (e.g. if MySQL normally uses 50% of memory, but you've just restarted it then it may only use 10% of memory right now, thus the recommendations will be very skewed)."
472 | echo
473 | }
474 | #
475 | # +---------------------------------------------------------------------+
476 | # | |
477 | # | /// END OF Other recommendations methods /// |
478 | # | |
479 | # +---------------------------------------------------------------------+
480 | #
481 | #============================================================================================
482 | #
483 | # +---------------------------------------------------------------------+
484 | # | |
485 | # | Debug methods |
486 | # | |
487 | # +---------------------------------------------------------------------+
488 | #
489 | # +---------------------------------------------------------------------+
490 | # | Print all variables for debugging |
491 | # +---------------------------------------------------------------------+
492 | function debug_print_variables()
493 | {
494 | echo "phpfpm_installed : $phpfpm_installed"
495 | echo "fpm_type : $fpm_type"
496 | echo "total_server_memory : $total_server_memory"
497 | }
498 | #
499 | # +---------------------------------------------------------------------+
500 | # | |
501 | # | /// END OF Debug methods /// |
502 | # | |
503 | # +---------------------------------------------------------------------+
504 | #
505 | #============================================================================================
506 | #
507 | # +---------------------------------------------------------------------+
508 | # | |
509 | # | Main methods |
510 | # | |
511 | # +---------------------------------------------------------------------+
512 | #
513 | # +---------------------------------------------------------------------+
514 | # | Verbose Main |
515 | # +---------------------------------------------------------------------+
516 | function main_verbose ()
517 | {
518 | # Run pre-checks
519 | check_php-fpm_installed_and_processname
520 | check_php-fpm_running
521 | check_bc_install
522 |
523 | # Print Logo
524 | print_logo
525 |
526 | # Run variable initialisation and general information gathering
527 | variable_initialisation
528 |
529 | # Display information for each PHP-FPM pool
530 | display_pool_information
531 |
532 | # Display information on server memory
533 | display_server_memory_information
534 |
535 | # Calculate and display PHP-FPM pool recommendations
536 | detailed_pool_recommendations
537 |
538 | # Print observations from the logfiles
539 | logfile_observations
540 |
541 | # Print general information about this script's usage
542 | general_script_info
543 | }
544 |
545 | # +---------------------------------------------------------------------+
546 | # | Succinct Main |
547 | # +---------------------------------------------------------------------+
548 | function main_succinct ()
549 | {
550 | # Run pre-checks
551 | check_php-fpm_installed_and_processname
552 | check_php-fpm_running
553 | check_bc_install
554 |
555 | # Run variable initialisation and general information gathering
556 | variable_initialisation
557 |
558 | # Display information for each PHP-FPM pool
559 | display_pool_information
560 |
561 | # Display information on server memory
562 | display_server_memory_information
563 |
564 | # Calculate and display PHP-FPM pool recommendations
565 | succinct_pool_recommendations
566 | }
567 |
568 | #
569 | # +---------------------------------------------------------------------+
570 | # | |
571 | # | /// END OF Main methods /// |
572 | # | |
573 | # +---------------------------------------------------------------------+
574 | #
575 | #============================================================================================
576 |
577 | if [ $# != 0 ]; then
578 | if [ $1 == "-d" ]; then
579 | main_verbose
580 | fi
581 | else
582 | main_succinct
583 | fi
584 |
--------------------------------------------------------------------------------
/php-fpmpal.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 |
3 | import platform
4 | import sys
5 | import os
6 | import glob
7 | import subprocess
8 |
9 | def execute_remotely(device):
10 | url = "https://raw.githubusercontent.com/pksteyn/php-fpmpal/master/php-fpmpal.01.sh"
11 | #cmd = "ot -C 'curl -s %s | python -' --sudo-make-me-a-sandwich %s" % (url, device)
12 | #cmd = "ht -C 'curl -s %s | bash -' --sudo-make-me-a-sandwich %s" % (url, device)
13 | cmd = "ht -C 'curl -s %s | bash' --sudo-make-me-a-sandwich %s" % (url, device)
14 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
15 | stderr=subprocess.PIPE, shell=True)
16 | output, err = p.communicate()
17 | if err:
18 | print err
19 | print output
20 |
21 |
22 | def main():
23 | try:
24 | commandline_arg = sys.argv[1]
25 | if commandline_arg:
26 | execute_remotely(commandline_arg)
27 | except IndexError:
28 | display_output()
29 |
30 | if __name__ == "__main__":
31 | main()
32 |
33 |
--------------------------------------------------------------------------------
/php-fpmpal.sh:
--------------------------------------------------------------------------------
1 | #! /bin/bash
2 |
3 | ### Switches
4 | function usage()
5 | {
6 | echo
7 | echo "Usage: php-fpmpal.sh [OPTION]"
8 | echo
9 | echo "Arguments:"
10 | echo " -MB [VALUE] Specify how much memory (in MB) you would like the whole of PHP-FPM to be able to use and base the calculations on this value, rather than on the value worked out by the script"
11 | echo
12 | }
13 |
14 |
15 | if [ $# != 0 ]; then
16 | if [ $1 == "--help" ]; then
17 | usage
18 | exit 0
19 | fi
20 | fi
21 |
22 | ### Check if the script is being run with root privileges
23 | if [ $EUID != "0" ]; then
24 | echo "This script must be run as root" 1>&2
25 | echo
26 | echo
27 | exit 1
28 | fi
29 |
30 | ### Check if bc is installed
31 | bc -v 1> /dev/null 2>&1
32 | if [ $? != 0 ]; then
33 | echo -e "\e[31m\"bc\" is not installed. This script depends on it.\e[0m"
34 | while true; do
35 | echo "Do you wish to install bc? (y/n)"
36 | read yn < /dev/tty
37 | case $yn in
38 | [Yy]* ) python -mplatform | egrep -i 'debian|ubuntu' 2>&1 > /dev/null && apt-get install bc -y -qq || yum install bc -y -q ; break;;
39 | [Nn]* ) exit;;
40 | * ) echo "Please answer yes or no.";;
41 | esac
42 | done
43 | fi
44 |
45 | ### END OF switches
46 |
47 | echo
48 | echo -e "\e[38;5;81m ( ) ( ( ( * "
49 | echo -e "\e[38;5;51m\e[1m )\ ) ( /( )\ ) )\ ) )\ ) ( \` \e[33m\e[1m ( "
50 | echo -e "\e[38;5;45m(()/( )\())(()/( (()/( (()/( )\))( \e[38;5;214m ) )\ "
51 | echo -e "\e[38;5;33m /(_))((_)\ /(_)) /(_)) /(_))((_)()\ \e[38;5;208m \` ) ( /( ((_) "
52 | echo -e "\e[38;5;27m(_)) _((_)(_)) (_))_|(_)) (_()((_) \e[38;5;124m/(/( )(_)) _ "
53 | echo -e "\e[0m\e[38;5;21m| _ \ | || || _ \ ___ | |_ | _ \ | \/ |\e[38;5;88m\e[1m((_)_\ \e[38;5;88m((_)\e[0m\e[38;5;9m_ | | "
54 | echo -ne "\e[38;5;21m| _/ | __ || _/|___|| __| | _/ | |\/| |\e[38;5;9m| '_ \\"
55 | echo -e "\e[38;5;88m\e[1m)\e[0m\e[38;5;9m/ _\` || | "
56 | echo -e "\e[38;5;21m|_| |_||_||_| |_| |_| |_| |_|\e[38;5;9m| .__/ \__,_||_| "
57 | for i in {17..21} {21..17} {17..21} {21..17} {17..21} {21..17} {17..21} {21..17} {17..17} ; do echo -en "\e[38;5;${i}m=\e[0m" ; done
58 | echo -en "\e[38;5;9m |_| "
59 | for i in {17..21} {21..21} {21..21} {21..21} {20..16} ; do echo -en "\e[38;5;${i}m=\e[0m" ; done
60 | echo -e "\e[0m"
61 | echo
62 | echo
63 |
64 |
65 | ### Determine whether the PHP-FPM process is called php-fpm or php5-fpm
66 | phpfpm_installed=0
67 |
68 | php-fpm -v 1> /dev/null 2>&1
69 | if [ $? == 0 ]; then
70 | phpfpm_installed=1
71 | fpm_type=`php-fpm -i 2>&1 | grep "SERVER\[\"_\"\]" | cut -d\/ -f4`
72 | fi
73 |
74 | php5-fpm -v 1> /dev/null 2>&1
75 | if [ $? == 0 ]; then
76 | phpfpm_installed=1
77 | fpm_type=`php5-fpm -i 2>&1 | grep "SERVER\[\"_\"\]" | cut -d\/ -f4`
78 | fi
79 |
80 | ### Exit if PHP-FPM is not installed
81 | if [ $phpfpm_installed == 0 ]; then
82 | echo -e "\e[31m!!! PHP-FPM not detected. Exiting. !!!\e[0m"
83 | echo
84 | exit 1
85 | fi
86 |
87 | ### Check if PHP-FPM is running
88 | ps aux | grep "php-fpm" | grep -v grep | grep -v "php-fpmpal" 1> /dev/null 2>&1
89 | if [ $? == 1 ]; then
90 | echo -e "\e[31m!!! PHP-FPM is installed but not running. PHP-FPM should be started before running this script. Exiting. !!!\e[0m"
91 | echo
92 | exit 1
93 | fi
94 |
95 | ### VARIABLE INITIALISATION
96 |
97 | # Apache root and config file
98 | #apachectl -V 1> /dev/null 2>&1
99 | if [ $(which apachectl | wc -l) != 0 ]; then
100 | httpd_root=`apachectl -V | awk -F "\"" '/HTTPD_ROOT/ {print $2}'`
101 | httpd_config_file=`apachectl -V | awk -F "\"" '/SERVER_CONFIG_FILE/ {print $2}'`
102 | httpd_root_and_config_file=$httpd_root"/"$httpd_config_file
103 | # Apache Includes (only 1 level deep from main configuration file)
104 | httpd_main_include=`awk -v var="$httpd_root" '/^Include/ {print var "/" $2}' $httpd_root_and_config_file`
105 | IFS=$'\n' list_of_apache_includes=($(ls $httpd_main_include))
106 | fi
107 | ### Get list of all PHP-FPM pools from process list
108 | IFS=$'\n' list_of_pools=($(ps aux | grep "php-fpm" | grep -v ^root | grep -v grep | awk -F "pool " '{print $2}' | sort | uniq | sed -e 's/ //g'))
109 |
110 | ### Get total server memory
111 | total_server_memory=`cat /proc/meminfo | grep MemTotal | awk '{print $2}'`
112 |
113 | ### Initialise variable to tally up total current PHP-FPM memory usage
114 | total_phpfpm_mem_usage=0
115 |
116 | ### Initialise variable to tally up total potential PHP-FPM memory usage based on largest processes in pool
117 | total_phpfpm_mem_usage_largest_process=0
118 |
119 | ### Initialise variable to tally up total potential PHP-FPM memory usage based on average process size in pool
120 | total_phpfpm_mem_usage_average_process=0
121 |
122 | ### Set variable to the total number of PHP-FPM pools
123 | let no_of_pools=(${#list_of_pools[@]}-1)
124 |
125 |
126 | ### Get list of possible configuration files
127 | IFS=$'\n' list_of_includes=($(grep -i ^include `ps aux | egrep "php.*master" | grep -v grep | cut -d\( -f2 | cut -d\) -f1` | cut -d= -f2))
128 |
129 | ### Set total number of include files
130 | let no_of_includes=(${#list_of_includes[@]}-1)
131 |
132 | ### Match each pool to its configuration file
133 | for ((p=0; p<=no_of_pools; p++))
134 | do
135 | for ((i=0; i<=no_of_includes; i++ ))
136 | do
137 | grep "\[${list_of_pools[$p]}\]" ${list_of_includes[$i]} > /dev/null
138 | if [ $? == 0 ]; then
139 | pool_config_file[$p]="${list_of_includes[$i]}"
140 | fi
141 | done
142 | done
143 |
144 |
145 | ### Give info re all list of PHP-FPM pools
146 | ### ======================================
147 |
148 | echo -en "\e[38;5;22m=\e[38;5;28m=\e[38;5;34m=\e[38;5;40m=\e[38;5;46m="
149 | echo -en "\e[32m\e[1m List of PHP-FPM pools \e[0m"
150 | echo -e "\e[38;5;46m=\e[38;5;40m=\e[38;5;34m=\e[38;5;28m=\e[38;5;22m=\e[0m"
151 |
152 | ### For each pool
153 | for ((i=0; i<=no_of_pools; i++))
154 | do
155 | ### Print pool name and configuration file
156 | echo -en "\e[36m\e[1m--- "
157 | echo -en ${list_of_pools[$i]}
158 | echo -e " ---\e[0m"
159 | echo -n "Configuration file: "; echo `grep -H "\[${list_of_pools[$i]}\]" ${pool_config_file[$i]} | cut -d: -f1`
160 |
161 |
162 | if [ 0 == 1 ]; then
163 | ### Find the site(s) that rely on this pool
164 | # Find the socket/TCP listener for this pool
165 | pool_listener=`egrep -i "^listen" ${pool_config_file[$i]} | sed -e 's/ //g' | grep "^listen=" | cut -d= -f2`
166 |
167 | # Find the FastCGIExternalServer directive for this socket in the Apache configuration and get the "Alias/Action"
168 | action_alias=`grep -h "FastCGIExternalServer" ${list_of_apache_includes[@]} | grep -v "#" | grep $pool_listener | awk '{print $2}'`
169 | #echo $action_alias
170 |
171 | # Find all the vhost files within which the "Alias/Action" is referenced
172 | # First find out whether any sites rely on this pool
173 | grep -H $action_alias ${list_of_apache_includes[@]} | grep Alias | grep -v "#" | awk '{print $4, $1}' | grep "$action_alias " > /dev/null
174 | # If no sites rely on it
175 | if [ $? == 1 ]; then
176 | echo "Site(s) that rely on this pool: None"
177 |
178 | # Else if at least one site relies on it
179 | else
180 | IFS=$'\n' vhost_name=($(grep -H $action_alias ${list_of_apache_includes[@]} | grep Alias | grep -v "#" | awk '{print $4, $1}' | grep "$action_alias " | awk '{print $2}' | cut -d: -f1))
181 | #echo ${vhost_name[@]}
182 |
183 | # As there may be multiple vhosts in the file, we want to extract just the vhost within which the "Alias/Action" was referenced
184 | echo "" > temp_filelist
185 | echo "" > filelist
186 |
187 | #action_line_nr=`grep -hn $action_alias$ ${list_of_apache_includes[@]} | grep Alias | grep -v "#" | cut -d: -f1`
188 | #echo $action_line_nr >> temp_filelist
189 |
190 | let no_of_vhosts_that_rely_on_this_pool=(${#vhost_name[@]}-1)
191 |
192 | #echo ${vhost_name[@]}
193 | #echo -n "nr_of_vhosts: "
194 | #echo $no_of_vhosts_that_rely_on_this_pool
195 |
196 | #nr_of_vhost_name=${#vhost_name[@]}
197 |
198 | #let nr_of_vhost_name=(${#vhost_name[@]}-1)
199 | #echo $nr_of_vhost_name
200 | # for every vhosts pull out the sitename
201 | echo -n "Site(s) that rely on this pool: "
202 | #for a in ${nr_of_vhost_name[@]} ###
203 | #for ((i=0; i<=no_of_vhosts_that_rely_on_this_pool; i++))
204 | for ((i=0; i<=no_of_vhosts_that_rely_on_this_pool; i++))
205 | #for ((i=0; i<=1; i++))
206 | do ###
207 | action_line_nr=`grep -hn $action_alias$ ${vhost_name[$i]} | grep Alias | grep -v "#" | cut -d: -f1`
208 |
209 | echo $action_line_nr >> temp_filelist
210 | egrep -n "> temp_filelist
211 | sort -n temp_filelist > filelist
212 | vhost_start_line=`grep -B1 $action_line_nr filelist | head -1`
213 | vhost_end_line=`grep -A1 $action_line_nr filelist | tail -1`
214 | rm -rf filelist
215 | rm -rf temp_filelist
216 |
217 | site_name=`sed -n "$vhost_start_line,$vhost_end_line p" ${vhost_name[$i]} | grep ServerName | awk '{print $2}'`
218 | echo -ne "\e[36m\e[1m$site_name\e[0m "
219 | done ###
220 | echo
221 | fi
222 | fi
223 |
224 |
225 |
226 |
227 | ### ROLLBACK
228 | if [ 0 == 1 ]; then
229 | ### Find the site(s) that rely on this pool
230 | # Find the socket/TCP listener for this pool
231 | pool_listener=`egrep -i "^listen" ${pool_config_file[$i]} | sed -e 's/ //g' | grep "^listen=" | cut -d= -f2`
232 |
233 | # Find the FastCGIExternalServer directive for this socket in the Apache configuration and get the "Alias/Action"
234 | action_alias=`grep -h "FastCGIExternalServer" ${list_of_apache_includes[@]} | grep -v "#" | grep $pool_listener | awk '{print $2}'`
235 |
236 | # Find the vhost file within which the "Alias/Action" is referenced
237 | vhost_name=`grep -H $action_alias ${list_of_apache_includes[@]} | grep Alias | cut -d: -f1`
238 |
239 | # As there may be multiple vhosts in the file, we want to extract just the vhost within which the "Alias/Action" was referenced
240 | echo "" > temp_filelist
241 | echo "" > filelist
242 |
243 | action_line_nr=`grep -hn $action_alias ${list_of_apache_includes[@]} | grep Alias | cut -d: -f1`
244 | echo $action_line_nr >> temp_filelist
245 |
246 | egrep -n "> temp_filelist
247 |
248 | sort -n temp_filelist > filelist
249 |
250 | vhost_start_line=`grep -B1 $action_line_nr filelist | head -1`
251 | vhost_end_line=`grep -A1 $action_line_nr filelist | tail -1`
252 |
253 | rm -rf filelist
254 | rm -rf temp_filelist
255 |
256 | echo -n "Site(s) that rely on this pool: "
257 | site_name=`sed -n "$vhost_start_line,$vhost_end_line p" $vhost_name | grep ServerName | awk '{print $2}'`
258 | echo -e "\e[36m$site_name\e[0m"
259 | fi
260 | ### /ROLLBACK
261 |
262 |
263 | ### Create a list of process IDs that belong to this pool
264 | IFS=$'\n' list_of_pids=($(ps aux | grep "php-fpm" | grep -v ^root | grep -v grep | grep pool | awk '{print $2, $13}' | grep " ${list_of_pools[$i]}$" | awk '{print $1}'))
265 |
266 | ### List all process IDs that belong to this pool
267 | echo -n "List of processes: "
268 | echo ${list_of_pids[@]}
269 |
270 | ### List total number of processes that belong to this pool
271 | echo -n "Number of processes: "
272 | echo ${#list_of_pids[@]}
273 |
274 | ### Get the current max_children value
275 | echo -n "Current max_children value: "
276 | current_max_children_value=`grep "^pm.max_children" ${pool_config_file[$i]} | cut -d= -f2 | sed -e 's/ //g' | tail -1`
277 | echo $current_max_children_value
278 |
279 | ### Calculate the total memory usage for this pool
280 | total_pool_mem_usage[$i]=0
281 | ### For each process
282 | for a in "${list_of_pids[@]}"
283 | do
284 | ### Gather memory usage (using pmap) and add it to total_pool_mem_usage variable
285 | let total_pool_mem_usage[$i]+=`pmap -d $a | grep "writeable/private" | awk '{ print $4 }' | sed -e s/K//`
286 | done
287 | ### Print total memory usage for this pool
288 | echo -n "Total memory usage for pool in KB: "
289 | echo ${total_pool_mem_usage[$i]}
290 | ### Increase the total PHP-FPM memory usage with the total memory usage for this pool
291 | let total_phpfpm_mem_usage+=${total_pool_mem_usage[$i]}
292 |
293 | ### Calculate the average process memory usage for this pool
294 | echo -n "Average memory usage per process in KB: "
295 | pool_ave_process_size[$i]=`echo "${total_pool_mem_usage[$i]} / ${#list_of_pids[@]}" | bc`
296 | echo ${pool_ave_process_size[$i]}
297 |
298 | ### Total potential memory usage for pool based on average process size: max_children * average process size
299 | echo -n "Total potential memory usage for pool (based on average process) (KB): "
300 | potential_mem_usage=`echo "$current_max_children_value * ${pool_ave_process_size[$i]}" | bc`
301 | let total_phpfpm_mem_usage_average_process+=$potential_mem_usage
302 | echo $potential_mem_usage
303 |
304 | ### Get the largest pool process size
305 | echo -n "Largest process in this pool is (KB): "
306 | for a in "${list_of_pids[@]}"
307 | do
308 | pool_processes_mem_size[$a]=`pmap -d $a | grep "writeable/private" | awk '{ print $4 }' | sed -e s/K// | sort -nr | head -1 | sort -nr | head -1`
309 | done
310 | let largest_pool_process_size=`echo "${pool_processes_mem_size[*]}" | sort -nr | head -1`
311 | echo $largest_pool_process_size
312 |
313 | ### Total potential memory usage for pool based on largest process size: max_children * largest process size
314 | echo -n "Total potential memory usage for pool (based on largest process) (KB): "
315 | potential_mem_usage=`echo "$current_max_children_value * $largest_pool_process_size" | bc`
316 | let total_phpfpm_mem_usage_largest_process+=$potential_mem_usage
317 | echo $potential_mem_usage
318 |
319 | echo
320 | done
321 |
322 | ### END OF Give info re all list of PHP-FPM pools
323 | ### =============================================
324 |
325 | ### Print out statistics re server memory usage
326 | ### ===========================================
327 |
328 | echo -en "\e[38;5;22m=\e[38;5;28m=\e[38;5;34m=\e[38;5;40m=\e[38;5;46m="
329 | echo -en "\e[32m\e[1m Server memory usage statistics \e[0m"
330 | echo -e "\e[38;5;46m=\e[38;5;40m=\e[38;5;34m=\e[38;5;28m=\e[38;5;22m=\e[0m"
331 |
332 | ### Total server memory
333 | echo -ne "Total server memory in KB: "
334 | echo -e "$total_server_memory"
335 |
336 | ### Calculate total Apache memory usage
337 | echo -n " =Total Apache memory usage in KB: "
338 | # If Apache is installed
339 | apachectl -V 1> /dev/null 2>&1
340 | if [ $? == 0 ]; then
341 | ### Gather the process name for Apache (httpd for RHEL/CentOS; apache2 for Ubuntu
342 | apache_process_name=`apachectl -V 2>&1 | grep PID | awk -F"run/" '{print $2}' | cut -d. -f1`
343 | ### If Apache is running
344 | ps aux | grep -v ^root | egrep $apache_process_name > /dev/null
345 | if [ $? == 0 ]; then
346 | ### Get a list of all Apache process IDs
347 | IFS=$'\n' list_of_apache_pids=($(ps aux | grep $apache_process_name | grep -v ^root | grep -v grep | awk '{print $2}'))
348 | total_apache_pool_mem_usage=0
349 | ### Add the memory usage for that process to the total Apache memory usage
350 | for a in "${list_of_apache_pids[@]}"
351 | do
352 | let total_apache_pool_mem_usage+=`pmap -d $a | grep "writeable/private" | awk '{ print $4 }' | sed -e s/K//`
353 | done
354 | ### Else if Apache is not running set total Apache memory usage to 0
355 | else
356 | total_apache_pool_mem_usage=0
357 | fi
358 | else
359 | total_apache_pool_mem_usage=0
360 | fi
361 | echo $total_apache_pool_mem_usage
362 |
363 | ### Calculate total nginx memory usage
364 | echo -n " =Total nginx memory usage in KB: "
365 | nginx -v 1> /dev/null 2>&1
366 | # If nginx is installed
367 | if [ $? == 0 ]; then
368 | IFS=$'\n' list_of_nginx_pids=($(ps aux | grep -i nginx | grep -v ^root | grep -v grep | awk '{print $2}'))
369 | total_nginx_mem_usage=0
370 | ### Add the memort usage for that process to the total nginx memory usage
371 | for a in "${list_of_nginx_pids[@]}"
372 | do
373 | let total_nginx_mem_usage+=`pmap -d $a | grep "writeable/private" | awk '{ print $4 }' | sed -e s/K//`
374 | done
375 | # else if nginx is not installed
376 | else
377 | total_nginx_mem_usage=0
378 | fi
379 | echo $total_nginx_mem_usage
380 |
381 | ### Calculate total Varnish memory usage
382 | echo -n " =Total Varnish memory usage in KB: "
383 | ### If Varnish is running
384 | ps aux | grep -v ^root | grep varnish > /dev/null
385 | if [ $? == 0 ]; then
386 | ### Get Varnish's % memory usage from ps (I've found this more reliable than using pmap
387 | varnish_perc=`ps aux | grep -v ^root | grep varnish | awk '{print $4}'`
388 | ### Calculate total Varnish memory usage
389 | total_varnish_mem_usage=`echo "$total_server_memory / 100 * $varnish_perc" | bc`
390 | ### Else if Varnish is not running set total Varnish memory usage to 0
391 | else
392 | total_varnish_mem_usage=0
393 | fi
394 | echo $total_varnish_mem_usage
395 |
396 | ### Calculate total MySQL memory usage
397 | echo -n " =Total MySQL memory usage in KB: "
398 | ### If MySQL is running
399 | ps aux | grep -v ^root | grep mysql > /dev/null
400 | if [ $? == 0 ]; then
401 | ### Get MySQL's % memory usage from ps (I've found this more reliable than using pmap
402 | mysql_perc=`ps aux | grep -v ^root | grep mysql | awk '{print $4}'`
403 | ### Calculate total MySQL memory usage
404 | total_mysql_pool_mem_usage=`echo "$total_server_memory / 100 * $mysql_perc" | bc`
405 | ### Else if MySQL is not running set total MySQL memory usage to 0
406 | else
407 | total_mysql_pool_mem_usage=0
408 | fi
409 | echo $total_mysql_pool_mem_usage
410 |
411 | ### Print total current PHP-FPM memory usage
412 | echo -n " =Total PHP-FPM memory usage in KB: "
413 | echo $total_phpfpm_mem_usage
414 |
415 | echo
416 |
417 | # Calculate total memory available to assign to PHP-FPM
418 | echo -n "Memory available to assign to PHP-FPM pools in KB: "
419 | ### Total memory usage - (the sum of all other processes listed above)
420 | ### Take total free memory and add current PHP-FPM total memory usage
421 | # RHEL 7's free reports look different to CentOS6, Ubuntu 14 and Debian 8 so I have to 1) check whether this is RHEL/CentOS 7, and 2) if it is, use different formulas
422 | rhel7_check=0
423 | if [ -f /etc/redhat-release ]; then
424 | rhel7_check=`grep -v ^# /etc/redhat-release | awk -F "release" '{print $2}' | awk '{print $1}' | cut -d. -f1` > /dev/null
425 | fi
426 | # If this is RHEL 7 then use this formula
427 | if [ $rhel7_check == '7' ]; then
428 | total_phpfpm_allowed_memory=$(echo "`free -k | awk '/Mem/ {print $7}'` + $total_phpfpm_mem_usage" | bc)
429 | # If this is not RHEL 7 use this formula
430 | else
431 | total_phpfpm_allowed_memory=$(echo "`free -k | awk '/buffers\/cache/ {print $4}'` + $total_phpfpm_mem_usage" | bc)
432 | fi
433 | # If the user has specified that they will set the available PHP-FPM memory themselves
434 | if [ $# != 0 ]; then
435 | # then print out user-specified value
436 | if [ $1 == "-MB" ]; then
437 | total_phpfpm_allowed_memory=`echo "$2*1024" | bc`
438 | echo -n $total_phpfpm_allowed_memory
439 | echo " (user-specified)"
440 | fi
441 | # Otherwise print out script-calculate value
442 | else
443 | echo -n $total_phpfpm_allowed_memory
444 | echo " (total free memory + PHP-FPM's current memory usage)"
445 | fi
446 | echo
447 |
448 |
449 | ### Print out total potential PHP-FPM usage based on largest process size per pool
450 | echo -n "Total potential PHP-FPM memory usage based on largest processes (KB): "
451 | echo -n $total_phpfpm_mem_usage_largest_process
452 | echo -ne "\e[33m (`echo "scale=2; $total_phpfpm_mem_usage_largest_process*100/$total_phpfpm_allowed_memory" | bc `"
453 | echo -n "%)"
454 | # Print warning if this is larger than allowed PHP-FPM memory usage
455 | if [ $total_phpfpm_mem_usage_largest_process -gt $total_phpfpm_allowed_memory ]; then
456 | echo -e " \e[31m!!! THIS IS LARGER THAN THE ASSIGNED MEMORY USAGE !!!\e[0m"
457 | else
458 | echo -e " \e[32m...GOOD :-)\e[0m"
459 | fi
460 |
461 | ### Print out total potential PHP-FPM usage based on average process size per pool
462 | echo -n "Total potential PHP-FPM memory usage based on average process size (KB): "
463 | echo -n $total_phpfpm_mem_usage_average_process
464 | echo -ne "\e[33m (`echo "scale=2; $total_phpfpm_mem_usage_average_process*100/$total_phpfpm_allowed_memory" | bc `"
465 | echo -n "%)"
466 | # Print warning if this is larger than allowed PHP-FPM memory usage
467 | if [ $total_phpfpm_mem_usage_average_process -gt $total_phpfpm_allowed_memory ]; then
468 | echo -e " \e[31m!!! THIS IS LARGER THAN THE ASSIGNED MEMORY USAGE !!!\e[0m"
469 | else
470 | echo -e " \e[32m...GOOD :-)\e[0m"
471 | fi
472 | echo
473 |
474 | ### END OF Print out statistics re server memory usage
475 | ### ==================================================
476 |
477 | ### Calculate and display recommendations for every PHP-FPM pool
478 | ### ============================================================
479 |
480 | echo -en "\e[38;5;22m=\e[38;5;28m=\e[38;5;34m=\e[38;5;40m=\e[38;5;46m="
481 | echo -en "\e[32m\e[1m Recommendations per pool \e[0m"
482 | echo -e "\e[38;5;46m=\e[38;5;40m=\e[38;5;34m=\e[38;5;28m=\e[38;5;22m=\e[0m"
483 |
484 | ### For every PHP-FPM pool
485 | for ((i=0; i<=no_of_pools; i++))
486 | do
487 | ### Calculate percentage of memory that pool is using against total PHP-FPM memory usage
488 | pool_perc_mem_use[$i]=`echo "scale=2; ${total_pool_mem_usage[$i]}*100/$total_phpfpm_mem_usage" | bc`
489 | ### Take that percentage and divide it by the total memory that should be available for PHP-FPM to use to get the total "allowed" memory usage for this pool
490 | pool_allowed_mem_use[$i]=`echo "$total_phpfpm_allowed_memory * ${pool_perc_mem_use[$i]} / 100" | bc`
491 | ### Divide the average process size for this pool by the total "allowed" memory usage for this pool to get a recommended max_children value
492 | pool_allowed_max_children[$i]=`echo "${pool_allowed_mem_use[$i]} / ${pool_ave_process_size[$i]}" | bc`
493 | ### Get the current max_children value for this PHP-FPM pool
494 | current_max_children_value=`grep "^pm.max_children" ${pool_config_file[$i]} | cut -d= -f2 | sed -e 's/ //g' | tail -1`
495 | ### Print out all this information
496 | echo -e "\e[36m\e[1m-- ${list_of_pools[$i]} --\e[0m currently uses ${total_pool_mem_usage[$i]} KB memory (\e[33m${pool_perc_mem_use[$i]}%\e[0m of all PHP-FPM memory usage). It should be allowed to use about ${pool_allowed_mem_use[$i]} KB of all available memory. Its average process size is ${pool_ave_process_size[$i]} KB so this means \e[38;5;208mmax_children should be set to ~\e[1m${pool_allowed_max_children[$i]}\e[0m. It is currently set to $current_max_children_value (this can be changed in ${pool_config_file[$i]})."
497 | done
498 |
499 | echo
500 | ### END OF Calculate and display recommendations for every PHP-FPM pool
501 | ### ===================================================================
502 |
503 |
504 | ### Other considerations
505 | ### ====================
506 | echo -en "\e[38;5;22m=\e[38;5;28m=\e[38;5;34m=\e[38;5;40m=\e[38;5;46m="
507 | echo -en "\e[32m\e[1m Other considerations to take into account \e[0m"
508 | echo -e "\e[38;5;46m=\e[38;5;40m=\e[38;5;34m=\e[38;5;28m=\e[38;5;22m=\e[0m"
509 |
510 | # Find PHP-FPM main configuration file
511 | main_phpfpm_config_file=`ps aux | grep "php-fpm: master process" | grep -v grep | awk -F 'process' '{print $2}' | sed -e 's/[() ]//g'`
512 |
513 | # Get error log location from main configuration file
514 | error_log_location=`grep "^error_log" $main_phpfpm_config_file | awk '{print $3}'`
515 |
516 | # Find out whether there were any max_children errors in the logs
517 | errors_in_logs=`zgrep "server reached pm.max_children" $error_log_location* | wc -l` > /dev/null
518 |
519 | # If there were max_children errors
520 | if [ $errors_in_logs != 0 ]; then
521 | # Print out consideration regarding this
522 | echo "From the PHP-FPM error logfiles ($error_log_location):"
523 | zgrep "server reached pm.max_children" $error_log_location* | awk '{print $5, $10}' | sed -e 's/[(),]//g' | sed -e 's/\]//g' | sort | uniq -c | awk '{print " - pool \033[36m" $2 "\033[0m had reached its max_children value of " $3 " on " $1 " occasion(s)"}'
524 | echo
525 | echo "For these pools you may want to compare the recommended max_children value to this information, and decide whether the recommended value would be high enough to prevent max_children from being hit in future."
526 | # Else print that there are no further recommendations
527 | else
528 | echo "No other recommendations at this stage."
529 | fi
530 |
531 | # PHP-FPMpal usage
532 | echo
533 | echo "Note: It is not ideal to run PHP-FPMpal shortly after restarting PHP-FPM or your webservices. This is because PHP-FPMpal makes recommendations based on the average pool process size, and if PHP-FPM was restarted a short while ago then the likelihood is high that there won't have been many requests made to the sites since the restart, and metrics will be skewed and not show a normalised average."
534 | echo "It is also worth noting that if you've recently restarted any services that normally use up a large amount of memory then you probably want to wait a while before running PHP-FPMpal (e.g. if MySQL normally uses 50% of memory, but you've just restarted it then it may only use 10% of memory right now, thus the recommendations will be very skewed)."
535 |
536 | echo
537 | ### END OF Other considerations
538 | ### ===========================
539 |
540 | for i in {17..21} {21..17} {17..21} {21..17} {17..21} {21..17} {17..21} {21..17} {17..21} {21..17} {17..21} {21..17} ; do echo -en "\e[38;5;${i}m=\e[0m" ; done
541 | echo -e "\e[0m"
542 | echo
543 |
--------------------------------------------------------------------------------