pax_global_header00006660000000000000000000000064125400333070014507gustar00rootroot0000000000000052 comment=c91d8eefde16bd047416409eb56353ea84a186e4 golang-procfs-0+git20150616.c91d8ee/000077500000000000000000000000001254003330700165165ustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/.travis.yml000066400000000000000000000000571254003330700206310ustar00rootroot00000000000000language: go go: - 1.3 - 1.4 - tip golang-procfs-0+git20150616.c91d8ee/AUTHORS.md000066400000000000000000000005201254003330700201620ustar00rootroot00000000000000The Prometheus project was started by Matt T. Proud (emeritus) and Julius Volz in 2012. Maintainers of this repository: * Tobias Schmidt The following individuals have contributed code to this repository (listed in alphabetical order): * Ji-Hoon, Seol * Tobias Schmidt golang-procfs-0+git20150616.c91d8ee/CONTRIBUTING.md000066400000000000000000000015331254003330700207510ustar00rootroot00000000000000# Contributing Prometheus uses GitHub to manage reviews of pull requests. * If you have a trivial fix or improvement, go ahead and create a pull request, addressing (with `@...`) one or more of the maintainers (see [AUTHORS.md](AUTHORS.md)) in the description of the pull request. * If you plan to do something more involved, first discuss your ideas on our [mailing list](https://groups.google.com/forum/?fromgroups#!forum/prometheus-developers). This will avoid unnecessary work and surely give you and us a good deal of inspiration. * Relevant coding style guidelines are the [Go Code Review Comments](https://code.google.com/p/go-wiki/wiki/CodeReviewComments) and the _Formatting and style_ section of Peter Bourgon's [Go: Best Practices for Production Environments](http://peter.bourgon.org/go-in-production/#formatting-and-style). golang-procfs-0+git20150616.c91d8ee/LICENSE000066400000000000000000000261351254003330700175320ustar00rootroot00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. golang-procfs-0+git20150616.c91d8ee/NOTICE000066400000000000000000000003551254003330700174250ustar00rootroot00000000000000procfs provides functions to retrieve system, kernel and process metrics from the pseudo-filesystem proc. Copyright 2014-2015 The Prometheus Authors This product includes software developed at SoundCloud Ltd. (http://soundcloud.com/). golang-procfs-0+git20150616.c91d8ee/README.md000066400000000000000000000010001254003330700177640ustar00rootroot00000000000000# procfs This procfs package provides functions to retrieve system, kernel and process metrics from the pseudo-filesystem proc. *WARNING*: This package is a work in progress. Its API may still break in backwards-incompatible ways without warnings. Use it at your own risk. [![GoDoc](https://godoc.org/github.com/prometheus/procfs?status.png)](https://godoc.org/github.com/prometheus/procfs) [![Build Status](https://travis-ci.org/prometheus/procfs.svg?branch=master)](https://travis-ci.org/prometheus/procfs) golang-procfs-0+git20150616.c91d8ee/doc.go000066400000000000000000000025041254003330700176130ustar00rootroot00000000000000// Copyright 2014 Prometheus Team // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // Package procfs provides functions to retrieve system, kernel and process // metrics from the pseudo-filesystem proc. // // Example: // // package main // // import ( // "fmt" // "log" // // "github.com/prometheus/procfs" // ) // // func main() { // p, err := procfs.Self() // if err != nil { // log.Fatalf("could not get process: %s", err) // } // // stat, err := p.NewStat() // if err != nil { // log.Fatalf("could not get process stat: %s", err) // } // // fmt.Printf("command: %s\n", stat.Comm) // fmt.Printf("cpu time: %fs\n", stat.CPUTime()) // fmt.Printf("vsize: %dB\n", stat.VirtualMemory()) // fmt.Printf("rss: %dB\n", stat.ResidentMemory()) // } // package procfs golang-procfs-0+git20150616.c91d8ee/fixtures/000077500000000000000000000000001254003330700203675ustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/26231/000077500000000000000000000000001254003330700210445ustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/26231/cmdline000066400000000000000000000000201254003330700223720ustar00rootroot00000000000000vimtest.go+10golang-procfs-0+git20150616.c91d8ee/fixtures/26231/exe000077700000000000000000000000001254003330700237212/usr/bin/vimustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/26231/fd/000077500000000000000000000000001254003330700214355ustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/26231/fd/0000077700000000000000000000000001254003330700256702../../symlinktargets/abcustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/26231/fd/1000077700000000000000000000000001254003330700257022../../symlinktargets/defustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/26231/fd/10000077700000000000000000000000001254003330700260562../../symlinktargets/xyzustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/26231/fd/2000077700000000000000000000000001254003330700257142../../symlinktargets/ghiustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/26231/fd/3000077700000000000000000000000001254003330700257672../../symlinktargets/uvwustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/26231/io000066400000000000000000000001641254003330700213770ustar00rootroot00000000000000rchar: 750339 wchar: 818609 syscr: 7405 syscw: 5245 read_bytes: 1024 write_bytes: 2048 cancelled_write_bytes: -1024 golang-procfs-0+git20150616.c91d8ee/fixtures/26231/limits000066400000000000000000000022751254003330700222760ustar00rootroot00000000000000Limit Soft Limit Hard Limit Units Max cpu time unlimited unlimited seconds Max file size unlimited unlimited bytes Max data size unlimited unlimited bytes Max stack size 8388608 unlimited bytes Max core file size 0 unlimited bytes Max resident set unlimited unlimited bytes Max processes 62898 62898 processes Max open files 2048 4096 files Max locked memory 65536 65536 bytes Max address space unlimited unlimited bytes Max file locks unlimited unlimited locks Max pending signals 62898 62898 signals Max msgqueue size 819200 819200 bytes Max nice priority 0 0 Max realtime priority 0 0 Max realtime timeout unlimited unlimited us golang-procfs-0+git20150616.c91d8ee/fixtures/26231/stat000066400000000000000000000005121254003330700217400ustar00rootroot0000000000000026231 (vim) R 5392 7446 5392 34835 7446 4218880 32533 309516 26 82 1677 44 158 99 20 0 1 0 82375 56274944 1981 18446744073709551615 4194304 6294284 140736914091744 140736914087944 139965136429984 0 0 12288 1870679807 0 0 0 17 0 0 0 31 0 0 8391624 8481048 16420864 140736914093252 140736914093279 140736914093279 140736914096107 0 golang-procfs-0+git20150616.c91d8ee/fixtures/26232/000077500000000000000000000000001254003330700210455ustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/26232/cmdline000066400000000000000000000000001254003330700223710ustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/26232/fd/000077500000000000000000000000001254003330700214365ustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/26232/fd/0000077700000000000000000000000001254003330700256712../../symlinktargets/abcustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/26232/fd/1000077700000000000000000000000001254003330700257032../../symlinktargets/defustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/26232/fd/2000077700000000000000000000000001254003330700257152../../symlinktargets/ghiustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/26232/fd/3000077700000000000000000000000001254003330700257702../../symlinktargets/uvwustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/26232/fd/4000077700000000000000000000000001254003330700260022../../symlinktargets/xyzustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/26232/limits000066400000000000000000000024531254003330700222750ustar00rootroot00000000000000Limit Soft Limit Hard Limit Units Max cpu time unlimited unlimited seconds Max file size unlimited unlimited bytes Max data size unlimited unlimited bytes Max stack size 8388608 unlimited bytes Max core file size 0 unlimited bytes Max resident set unlimited unlimited bytes Max processes 29436 29436 processes Max open files 1024 4096 files Max locked memory 65536 65536 bytes Max address space unlimited unlimited bytes Max file locks unlimited unlimited locks Max pending signals 29436 29436 signals Max msgqueue size 819200 819200 bytes Max nice priority 0 0 Max realtime priority 0 0 Max realtime timeout unlimited unlimited us golang-procfs-0+git20150616.c91d8ee/fixtures/26232/stat000066400000000000000000000002531254003330700217430ustar00rootroot0000000000000033 (ata_sff) S 2 0 0 0 -1 69238880 0 0 0 0 0 0 0 0 0 -20 1 0 5 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 18446744073709551615 0 0 17 1 0 0 0 0 0 0 0 0 0 0 0 0 0 golang-procfs-0+git20150616.c91d8ee/fixtures/584/000077500000000000000000000000001254003330700207075ustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/584/stat000066400000000000000000000005251254003330700216070ustar00rootroot000000000000001020 ((a b ) ( c d) ) R 28378 1020 28378 34842 1020 4218880 286 0 0 0 0 0 0 0 20 0 1 0 10839175 10395648 155 18446744073709551615 4194304 4238788 140736466511168 140736466511168 140609271124624 0 0 0 0 0 0 0 17 5 0 0 0 0 0 6336016 6337300 25579520 140736466515030 140736466515061 140736466515061 140736466518002 0 #!/bin/cat /proc/self/stat golang-procfs-0+git20150616.c91d8ee/fixtures/net/000077500000000000000000000000001254003330700211555ustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/net/ip_vs000066400000000000000000000013021254003330700222140ustar00rootroot00000000000000IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP C0A80016:0CEA wlc -> C0A85216:0CEA Tunnel 100 248 2 -> C0A85318:0CEA Tunnel 100 248 2 -> C0A85315:0CEA Tunnel 100 248 1 TCP C0A80039:0CEA wlc -> C0A85416:0CEA Tunnel 0 0 0 -> C0A85215:0CEA Tunnel 100 1499 0 -> C0A83215:0CEA Tunnel 100 1498 0 TCP C0A80037:0CEA wlc -> C0A8321A:0CEA Tunnel 0 0 0 -> C0A83120:0CEA Tunnel 100 0 0 golang-procfs-0+git20150616.c91d8ee/fixtures/net/ip_vs_stats000066400000000000000000000004621254003330700234400ustar00rootroot00000000000000 Total Incoming Outgoing Incoming Outgoing Conns Packets Packets Bytes Bytes 16AA370 E33656E5 0 51D8C8883AB3 0 Conns/s Pkts/s Pkts/s Bytes/s Bytes/s 4 1FB3C 0 1282A8F 0 golang-procfs-0+git20150616.c91d8ee/fixtures/stat000066400000000000000000000040561254003330700212720ustar00rootroot00000000000000cpu 301854 612 111922 8979004 3552 2 3944 0 0 0 cpu0 44490 19 21045 1087069 220 1 3410 0 0 0 cpu1 47869 23 16474 1110787 591 0 46 0 0 0 cpu2 46504 36 15916 1112321 441 0 326 0 0 0 cpu3 47054 102 15683 1113230 533 0 60 0 0 0 cpu4 28413 25 10776 1140321 217 0 8 0 0 0 cpu5 29271 101 11586 1136270 672 0 30 0 0 0 cpu6 29152 36 10276 1139721 319 0 29 0 0 0 cpu7 29098 268 10164 1139282 555 0 31 0 0 0 intr 8885917 17 0 0 0 0 0 0 0 1 79281 0 0 0 0 0 0 0 231237 0 0 0 0 250586 103 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 223424 190745 13 906 1283803 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ctxt 38014093 btime 1418183276 processes 26442 procs_running 2 procs_blocked 0 softirq 5057579 250191 1481983 1647 211099 186066 0 1783454 622196 12499 508444 golang-procfs-0+git20150616.c91d8ee/fixtures/symlinktargets/000077500000000000000000000000001254003330700234475ustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/symlinktargets/README000066400000000000000000000002211254003330700243220ustar00rootroot00000000000000This directory contains some empty files that are the symlinks the files in the "fd" directory point to. They are otherwise ignored by the tests golang-procfs-0+git20150616.c91d8ee/fixtures/symlinktargets/abc000066400000000000000000000000001254003330700241050ustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/symlinktargets/def000066400000000000000000000000001254003330700241160ustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/symlinktargets/ghi000066400000000000000000000000001254003330700241270ustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/symlinktargets/uvw000066400000000000000000000000001254003330700242010ustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fixtures/symlinktargets/xyz000066400000000000000000000000001254003330700242120ustar00rootroot00000000000000golang-procfs-0+git20150616.c91d8ee/fs.go000066400000000000000000000017061254003330700174610ustar00rootroot00000000000000package procfs import ( "fmt" "os" "path" ) // FS represents the pseudo-filesystem proc, which provides an interface to // kernel data structures. type FS string // DefaultMountPoint is the common mount point of the proc filesystem. const DefaultMountPoint = "/proc" // NewFS returns a new FS mounted under the given mountPoint. It will error // if the mount point can't be read. func NewFS(mountPoint string) (FS, error) { info, err := os.Stat(mountPoint) if err != nil { return "", fmt.Errorf("could not read %s: %s", mountPoint, err) } if !info.IsDir() { return "", fmt.Errorf("mount point %s is not a directory", mountPoint) } return FS(mountPoint), nil } func (fs FS) stat(p string) (os.FileInfo, error) { return os.Stat(path.Join(string(fs), p)) } func (fs FS) open(p string) (*os.File, error) { return os.Open(path.Join(string(fs), p)) } func (fs FS) readlink(p string) (string, error) { return os.Readlink(path.Join(string(fs), p)) } golang-procfs-0+git20150616.c91d8ee/fs_test.go000066400000000000000000000004441254003330700205160ustar00rootroot00000000000000package procfs import "testing" func TestNewFS(t *testing.T) { if _, err := NewFS("foobar"); err == nil { t.Error("want NewFS to fail for non-existing mount point") } if _, err := NewFS("procfs.go"); err == nil { t.Error("want NewFS to fail if mount point is not a directory") } } golang-procfs-0+git20150616.c91d8ee/ipvs.go000066400000000000000000000124431254003330700200320ustar00rootroot00000000000000package procfs import ( "bufio" "encoding/hex" "errors" "fmt" "io" "io/ioutil" "net" "strconv" "strings" ) // IPVSStats holds IPVS statistics, as exposed by the kernel in `/proc/net/ip_vs_stats`. type IPVSStats struct { // Total count of connections. Connections uint64 // Total incoming packages processed. IncomingPackets uint64 // Total outgoing packages processed. OutgoingPackets uint64 // Total incoming traffic. IncomingBytes uint64 // Total outgoing traffic. OutgoingBytes uint64 } // IPVSBackendStatus holds current metrics of one virtual / real address pair. type IPVSBackendStatus struct { // The local (virtual) IP address. LocalAddress net.IP // The local (virtual) port. LocalPort uint16 // The transport protocol (TCP, UDP). Proto string // The remote (real) IP address. RemoteAddress net.IP // The remote (real) port. RemotePort uint16 // The current number of active connections for this virtual/real address pair. ActiveConn uint64 // The current number of inactive connections for this virtual/real address pair. InactConn uint64 // The current weight of this virtual/real address pair. Weight uint64 } // NewIPVSStats reads the IPVS statistics. func NewIPVSStats() (IPVSStats, error) { fs, err := NewFS(DefaultMountPoint) if err != nil { return IPVSStats{}, err } return fs.NewIPVSStats() } // NewIPVSStats reads the IPVS statistics from the specified `proc` filesystem. func (fs FS) NewIPVSStats() (IPVSStats, error) { file, err := fs.open("net/ip_vs_stats") if err != nil { return IPVSStats{}, err } defer file.Close() return parseIPVSStats(file) } // parseIPVSStats performs the actual parsing of `ip_vs_stats`. func parseIPVSStats(file io.Reader) (IPVSStats, error) { var ( statContent []byte statLines []string statFields []string stats IPVSStats ) statContent, err := ioutil.ReadAll(file) if err != nil { return IPVSStats{}, err } statLines = strings.SplitN(string(statContent), "\n", 4) if len(statLines) != 4 { return IPVSStats{}, errors.New("ip_vs_stats corrupt: too short") } statFields = strings.Fields(statLines[2]) if len(statFields) != 5 { return IPVSStats{}, errors.New("ip_vs_stats corrupt: unexpected number of fields") } stats.Connections, err = strconv.ParseUint(statFields[0], 16, 64) if err != nil { return IPVSStats{}, err } stats.IncomingPackets, err = strconv.ParseUint(statFields[1], 16, 64) if err != nil { return IPVSStats{}, err } stats.OutgoingPackets, err = strconv.ParseUint(statFields[2], 16, 64) if err != nil { return IPVSStats{}, err } stats.IncomingBytes, err = strconv.ParseUint(statFields[3], 16, 64) if err != nil { return IPVSStats{}, err } stats.OutgoingBytes, err = strconv.ParseUint(statFields[4], 16, 64) if err != nil { return IPVSStats{}, err } return stats, nil } // NewIPVSBackendStatus reads and returns the status of all (virtual,real) server pairs. func NewIPVSBackendStatus() ([]IPVSBackendStatus, error) { fs, err := NewFS(DefaultMountPoint) if err != nil { return []IPVSBackendStatus{}, err } return fs.NewIPVSBackendStatus() } // NewIPVSBackendStatus reads and returns the status of all (virtual,real) server pairs from the specified `proc` filesystem. func (fs FS) NewIPVSBackendStatus() ([]IPVSBackendStatus, error) { file, err := fs.open("net/ip_vs") if err != nil { return nil, err } defer file.Close() return parseIPVSBackendStatus(file) } func parseIPVSBackendStatus(file io.Reader) ([]IPVSBackendStatus, error) { var ( status []IPVSBackendStatus scanner = bufio.NewScanner(file) proto string localAddress net.IP localPort uint16 err error ) for scanner.Scan() { fields := strings.Fields(string(scanner.Text())) if len(fields) == 0 { continue } switch { case fields[0] == "IP" || fields[0] == "Prot" || fields[1] == "RemoteAddress:Port": continue case fields[0] == "TCP" || fields[0] == "UDP": if len(fields) < 2 { continue } proto = fields[0] localAddress, localPort, err = parseIPPort(fields[1]) if err != nil { return nil, err } case fields[0] == "->": if len(fields) < 6 { continue } remoteAddress, remotePort, err := parseIPPort(fields[1]) if err != nil { return nil, err } weight, err := strconv.ParseUint(fields[3], 10, 64) if err != nil { return nil, err } activeConn, err := strconv.ParseUint(fields[4], 10, 64) if err != nil { return nil, err } inactConn, err := strconv.ParseUint(fields[5], 10, 64) if err != nil { return nil, err } status = append(status, IPVSBackendStatus{ LocalAddress: localAddress, LocalPort: localPort, RemoteAddress: remoteAddress, RemotePort: remotePort, Proto: proto, Weight: weight, ActiveConn: activeConn, InactConn: inactConn, }) } } return status, nil } func parseIPPort(s string) (net.IP, uint16, error) { tmp := strings.SplitN(s, ":", 2) if len(tmp) != 2 { return nil, 0, fmt.Errorf("invalid IP:Port: %s", s) } if len(tmp[0]) != 8 && len(tmp[0]) != 32 { return nil, 0, fmt.Errorf("invalid IP: %s", tmp[0]) } ip, err := hex.DecodeString(tmp[0]) if err != nil { return nil, 0, err } port, err := strconv.ParseUint(tmp[1], 16, 16) if err != nil { return nil, 0, err } return ip, uint16(port), nil } golang-procfs-0+git20150616.c91d8ee/ipvs_test.go000066400000000000000000000117611254003330700210730ustar00rootroot00000000000000package procfs import ( "net" "testing" ) var ( expectedIPVSStats = IPVSStats{ Connections: 23765872, IncomingPackets: 3811989221, OutgoingPackets: 0, IncomingBytes: 89991519156915, OutgoingBytes: 0, } expectedIPVSBackendStatuses = []IPVSBackendStatus{ IPVSBackendStatus{ LocalAddress: net.ParseIP("192.168.0.22"), LocalPort: 3306, RemoteAddress: net.ParseIP("192.168.82.22"), RemotePort: 3306, Proto: "TCP", Weight: 100, ActiveConn: 248, InactConn: 2, }, IPVSBackendStatus{ LocalAddress: net.ParseIP("192.168.0.22"), LocalPort: 3306, RemoteAddress: net.ParseIP("192.168.83.24"), RemotePort: 3306, Proto: "TCP", Weight: 100, ActiveConn: 248, InactConn: 2, }, IPVSBackendStatus{ LocalAddress: net.ParseIP("192.168.0.22"), LocalPort: 3306, RemoteAddress: net.ParseIP("192.168.83.21"), RemotePort: 3306, Proto: "TCP", Weight: 100, ActiveConn: 248, InactConn: 1, }, IPVSBackendStatus{ LocalAddress: net.ParseIP("192.168.0.57"), LocalPort: 3306, RemoteAddress: net.ParseIP("192.168.84.22"), RemotePort: 3306, Proto: "TCP", Weight: 0, ActiveConn: 0, InactConn: 0, }, IPVSBackendStatus{ LocalAddress: net.ParseIP("192.168.0.57"), LocalPort: 3306, RemoteAddress: net.ParseIP("192.168.82.21"), RemotePort: 3306, Proto: "TCP", Weight: 100, ActiveConn: 1499, InactConn: 0, }, IPVSBackendStatus{ LocalAddress: net.ParseIP("192.168.0.57"), LocalPort: 3306, RemoteAddress: net.ParseIP("192.168.50.21"), RemotePort: 3306, Proto: "TCP", Weight: 100, ActiveConn: 1498, InactConn: 0, }, IPVSBackendStatus{ LocalAddress: net.ParseIP("192.168.0.55"), LocalPort: 3306, RemoteAddress: net.ParseIP("192.168.50.26"), RemotePort: 3306, Proto: "TCP", Weight: 0, ActiveConn: 0, InactConn: 0, }, IPVSBackendStatus{ LocalAddress: net.ParseIP("192.168.0.55"), LocalPort: 3306, RemoteAddress: net.ParseIP("192.168.49.32"), RemotePort: 3306, Proto: "TCP", Weight: 100, ActiveConn: 0, InactConn: 0, }, } ) func TestIPVSStats(t *testing.T) { fs, err := NewFS("fixtures") if err != nil { t.Fatal(err) } stats, err := fs.NewIPVSStats() if err != nil { t.Fatal(err) } if stats != expectedIPVSStats { t.Errorf("want %+v, got %+v", expectedIPVSStats, stats) } } func TestParseIPPort(t *testing.T) { ip := net.ParseIP("192.168.0.22") port := uint16(3306) gotIP, gotPort, err := parseIPPort("C0A80016:0CEA") if err != nil { t.Fatal(err) } if !(gotIP.Equal(ip) && port == gotPort) { t.Errorf("want %s:%d, got %s:%d", ip, port, gotIP, gotPort) } } func TestParseIPPortInvalid(t *testing.T) { testcases := []string{ "", "C0A80016", "C0A800:1234", "FOOBARBA:1234", "C0A80016:0CEA:1234", } for _, s := range testcases { ip, port, err := parseIPPort(s) if ip != nil || port != uint16(0) || err == nil { t.Errorf("Expected error for input %s, got ip = %s, port = %v, err = %v", s, ip, port, err) } } } func TestParseIPPortIPv6(t *testing.T) { ip := net.ParseIP("dead:beef::1") port := uint16(8080) gotIP, gotPort, err := parseIPPort("DEADBEEF000000000000000000000001:1F90") if err != nil { t.Fatal(err) } if !(gotIP.Equal(ip) && port == gotPort) { t.Errorf("want %s:%d, got %s:%d", ip, port, gotIP, gotPort) } } func TestIPVSBackendStatus(t *testing.T) { fs, err := NewFS("fixtures") if err != nil { t.Fatal(err) } backendStats, err := fs.NewIPVSBackendStatus() if err != nil { t.Fatal(err) } for idx, expect := range expectedIPVSBackendStatuses { if !backendStats[idx].LocalAddress.Equal(expect.LocalAddress) { t.Errorf("expected LocalAddress %s, got %s", expect.LocalAddress, backendStats[idx].LocalAddress) } if backendStats[idx].LocalPort != expect.LocalPort { t.Errorf("expected LocalPort %d, got %d", expect.LocalPort, backendStats[idx].LocalPort) } if !backendStats[idx].RemoteAddress.Equal(expect.RemoteAddress) { t.Errorf("expected RemoteAddress %s, got %s", expect.RemoteAddress, backendStats[idx].RemoteAddress) } if backendStats[idx].RemotePort != expect.RemotePort { t.Errorf("expected RemotePort %d, got %d", expect.RemotePort, backendStats[idx].RemotePort) } if backendStats[idx].Proto != expect.Proto { t.Errorf("expected Proto %s, got %s", expect.Proto, backendStats[idx].Proto) } if backendStats[idx].Weight != expect.Weight { t.Errorf("expected Weight %d, got %d", expect.Weight, backendStats[idx].Weight) } if backendStats[idx].ActiveConn != expect.ActiveConn { t.Errorf("expected ActiveConn %d, got %d", expect.ActiveConn, backendStats[idx].ActiveConn) } if backendStats[idx].InactConn != expect.InactConn { t.Errorf("expected InactConn %d, got %d", expect.InactConn, backendStats[idx].InactConn) } } } golang-procfs-0+git20150616.c91d8ee/proc.go000066400000000000000000000075601254003330700200200ustar00rootroot00000000000000package procfs import ( "fmt" "io/ioutil" "os" "path" "strconv" "strings" ) // Proc provides information about a running process. type Proc struct { // The process ID. PID int fs FS } // Procs represents a list of Proc structs. type Procs []Proc func (p Procs) Len() int { return len(p) } func (p Procs) Swap(i, j int) { p[i], p[j] = p[j], p[i] } func (p Procs) Less(i, j int) bool { return p[i].PID < p[j].PID } // Self returns a process for the current process. func Self() (Proc, error) { return NewProc(os.Getpid()) } // NewProc returns a process for the given pid under /proc. func NewProc(pid int) (Proc, error) { fs, err := NewFS(DefaultMountPoint) if err != nil { return Proc{}, err } return fs.NewProc(pid) } // AllProcs returns a list of all currently avaible processes under /proc. func AllProcs() (Procs, error) { fs, err := NewFS(DefaultMountPoint) if err != nil { return Procs{}, err } return fs.AllProcs() } // NewProc returns a process for the given pid. func (fs FS) NewProc(pid int) (Proc, error) { if _, err := fs.stat(strconv.Itoa(pid)); err != nil { return Proc{}, err } return Proc{PID: pid, fs: fs}, nil } // AllProcs returns a list of all currently avaible processes. func (fs FS) AllProcs() (Procs, error) { d, err := fs.open("") if err != nil { return Procs{}, err } defer d.Close() names, err := d.Readdirnames(-1) if err != nil { return Procs{}, fmt.Errorf("could not read %s: %s", d.Name(), err) } p := Procs{} for _, n := range names { pid, err := strconv.ParseInt(n, 10, 64) if err != nil { continue } p = append(p, Proc{PID: int(pid), fs: fs}) } return p, nil } // CmdLine returns the command line of a process. func (p Proc) CmdLine() ([]string, error) { f, err := p.open("cmdline") if err != nil { return nil, err } defer f.Close() data, err := ioutil.ReadAll(f) if err != nil { return nil, err } if len(data) < 1 { return []string{}, nil } return strings.Split(string(data[:len(data)-1]), string(byte(0))), nil } // Executable returns the absolute path of the executable command of a process. func (p Proc) Executable() (string, error) { exe, err := p.readlink("exe") if os.IsNotExist(err) { return "", nil } return exe, err } // FileDescriptors returns the currently open file descriptors of a process. func (p Proc) FileDescriptors() ([]uintptr, error) { names, err := p.fileDescriptors() if err != nil { return nil, err } fds := make([]uintptr, len(names)) for i, n := range names { fd, err := strconv.ParseInt(n, 10, 32) if err != nil { return nil, fmt.Errorf("could not parse fd %s: %s", n, err) } fds[i] = uintptr(fd) } return fds, nil } // FileDescriptorTargets returns the targets of all file descriptors of a process. // If a file descriptor is not a symlink to a file (like a socket), that value will be the empty string. func (p Proc) FileDescriptorTargets() ([]string, error) { names, err := p.fileDescriptors() if err != nil { return nil, err } targets := make([]string, len(names)) for i, name := range names { target, err := p.readlink("fd/" + name) if err == nil { targets[i] = target } } return targets, nil } // FileDescriptorsLen returns the number of currently open file descriptors of // a process. func (p Proc) FileDescriptorsLen() (int, error) { fds, err := p.fileDescriptors() if err != nil { return 0, err } return len(fds), nil } func (p Proc) fileDescriptors() ([]string, error) { d, err := p.open("fd") if err != nil { return nil, err } defer d.Close() names, err := d.Readdirnames(-1) if err != nil { return nil, fmt.Errorf("could not read %s: %s", d.Name(), err) } return names, nil } func (p Proc) open(pa string) (*os.File, error) { return p.fs.open(path.Join(strconv.Itoa(p.PID), pa)) } func (p Proc) readlink(pa string) (string, error) { return p.fs.readlink(path.Join(strconv.Itoa(p.PID), pa)) } golang-procfs-0+git20150616.c91d8ee/proc_io.go000066400000000000000000000021421254003330700204760ustar00rootroot00000000000000package procfs import ( "fmt" "io/ioutil" ) // ProcIO models the content of /proc//io. type ProcIO struct { // Chars read. RChar uint64 // Chars written. WChar uint64 // Read syscalls. SyscR uint64 // Write syscalls. SyscW uint64 // Bytes read. ReadBytes uint64 // Bytes written. WriteBytes uint64 // Bytes written, but taking into account truncation. See // Documentation/filesystems/proc.txt in the kernel sources for // detailed explanation. CancelledWriteBytes int64 } // NewIO creates a new ProcIO instance from a given Proc instance. func (p Proc) NewIO() (ProcIO, error) { pio := ProcIO{} f, err := p.open("io") if err != nil { return pio, err } defer f.Close() data, err := ioutil.ReadAll(f) if err != nil { return pio, err } ioFormat := "rchar: %d\nwchar: %d\nsyscr: %d\nsyscw: %d\n" + "read_bytes: %d\nwrite_bytes: %d\n" + "cancelled_write_bytes: %d\n" _, err = fmt.Sscanf(string(data), ioFormat, &pio.RChar, &pio.WChar, &pio.SyscR, &pio.SyscW, &pio.ReadBytes, &pio.WriteBytes, &pio.CancelledWriteBytes) if err != nil { return pio, err } return pio, nil } golang-procfs-0+git20150616.c91d8ee/proc_io_test.go000066400000000000000000000017411254003330700215410ustar00rootroot00000000000000package procfs import "testing" func TestProcIO(t *testing.T) { fs, err := NewFS("fixtures") if err != nil { t.Fatal(err) } p, err := fs.NewProc(26231) if err != nil { t.Fatal(err) } s, err := p.NewIO() if err != nil { t.Fatal(err) } for _, test := range []struct { name string want uint64 got uint64 }{ {name: "RChar", want: 750339, got: s.RChar}, {name: "WChar", want: 818609, got: s.WChar}, {name: "SyscR", want: 7405, got: s.SyscR}, {name: "SyscW", want: 5245, got: s.SyscW}, {name: "ReadBytes", want: 1024, got: s.ReadBytes}, {name: "WriteBytes", want: 2048, got: s.WriteBytes}, } { if test.want != test.got { t.Errorf("want %s %d, got %d", test.name, test.want, test.got) } } for _, test := range []struct { name string want int64 got int64 }{ {name: "CancelledWriteBytes", want: -1024, got: s.CancelledWriteBytes}, } { if test.want != test.got { t.Errorf("want %s %d, got %d", test.name, test.want, test.got) } } } golang-procfs-0+git20150616.c91d8ee/proc_limits.go000066400000000000000000000050171254003330700213740ustar00rootroot00000000000000package procfs import ( "bufio" "fmt" "regexp" "strconv" ) // ProcLimits represents the soft limits for each of the process's resource // limits. type ProcLimits struct { CPUTime int FileSize int DataSize int StackSize int CoreFileSize int ResidentSet int Processes int OpenFiles int LockedMemory int AddressSpace int FileLocks int PendingSignals int MsqqueueSize int NicePriority int RealtimePriority int RealtimeTimeout int } const ( limitsFields = 3 limitsUnlimited = "unlimited" ) var ( limitsDelimiter = regexp.MustCompile(" +") ) // NewLimits returns the current soft limits of the process. func (p Proc) NewLimits() (ProcLimits, error) { f, err := p.open("limits") if err != nil { return ProcLimits{}, err } defer f.Close() var ( l = ProcLimits{} s = bufio.NewScanner(f) ) for s.Scan() { fields := limitsDelimiter.Split(s.Text(), limitsFields) if len(fields) != limitsFields { return ProcLimits{}, fmt.Errorf( "couldn't parse %s line %s", f.Name(), s.Text()) } switch fields[0] { case "Max cpu time": l.CPUTime, err = parseInt(fields[1]) case "Max file size": l.FileLocks, err = parseInt(fields[1]) case "Max data size": l.DataSize, err = parseInt(fields[1]) case "Max stack size": l.StackSize, err = parseInt(fields[1]) case "Max core file size": l.CoreFileSize, err = parseInt(fields[1]) case "Max resident set": l.ResidentSet, err = parseInt(fields[1]) case "Max processes": l.Processes, err = parseInt(fields[1]) case "Max open files": l.OpenFiles, err = parseInt(fields[1]) case "Max locked memory": l.LockedMemory, err = parseInt(fields[1]) case "Max address space": l.AddressSpace, err = parseInt(fields[1]) case "Max file locks": l.FileLocks, err = parseInt(fields[1]) case "Max pending signals": l.PendingSignals, err = parseInt(fields[1]) case "Max msgqueue size": l.MsqqueueSize, err = parseInt(fields[1]) case "Max nice priority": l.NicePriority, err = parseInt(fields[1]) case "Max realtime priority": l.RealtimePriority, err = parseInt(fields[1]) case "Max realtime timeout": l.RealtimeTimeout, err = parseInt(fields[1]) } if err != nil { return ProcLimits{}, err } } return l, s.Err() } func parseInt(s string) (int, error) { if s == limitsUnlimited { return -1, nil } i, err := strconv.ParseInt(s, 10, 32) if err != nil { return 0, fmt.Errorf("couldn't parse value %s: %s", s, err) } return int(i), nil } golang-procfs-0+git20150616.c91d8ee/proc_limits_test.go000066400000000000000000000013221254003330700224260ustar00rootroot00000000000000package procfs import "testing" func TestNewLimits(t *testing.T) { fs, err := NewFS("fixtures") if err != nil { t.Fatal(err) } p, err := fs.NewProc(26231) if err != nil { t.Fatal(err) } l, err := p.NewLimits() if err != nil { t.Fatal(err) } for _, test := range []struct { name string want int got int }{ {name: "cpu time", want: -1, got: l.CPUTime}, {name: "open files", want: 2048, got: l.OpenFiles}, {name: "msgqueue size", want: 819200, got: l.MsqqueueSize}, {name: "nice priority", want: 0, got: l.NicePriority}, {name: "address space", want: -1, got: l.AddressSpace}, } { if test.want != test.got { t.Errorf("want %s %d, got %d", test.name, test.want, test.got) } } } golang-procfs-0+git20150616.c91d8ee/proc_stat.go000066400000000000000000000110731254003330700210450ustar00rootroot00000000000000package procfs import ( "bytes" "fmt" "io/ioutil" "os" ) // Originally, this USER_HZ value was dynamically retrieved via a sysconf call which // required cgo. However, that caused a lot of problems regarding // cross-compilation. Alternatives such as running a binary to determine the // value, or trying to derive it in some other way were all problematic. // After much research it was determined that USER_HZ is actually hardcoded to // 100 on all Go-supported platforms as of the time of this writing. This is // why we decided to hardcode it here as well. It is not impossible that there // could be systems with exceptions, but they should be very exotic edge cases, // and in that case, the worst outcome will be two misreported metrics. // // See also the following discussions: // // - https://github.com/prometheus/node_exporter/issues/52 // - https://github.com/prometheus/procfs/pull/2 // - http://stackoverflow.com/questions/17410841/how-does-user-hz-solve-the-jiffy-scaling-issue const userHZ = 100 // ProcStat provides status information about the process, // read from /proc/[pid]/stat. type ProcStat struct { // The process ID. PID int // The filename of the executable. Comm string // The process state. State string // The PID of the parent of this process. PPID int // The process group ID of the process. PGRP int // The session ID of the process. Session int // The controlling terminal of the process. TTY int // The ID of the foreground process group of the controlling terminal of // the process. TPGID int // The kernel flags word of the process. Flags uint // The number of minor faults the process has made which have not required // loading a memory page from disk. MinFlt uint // The number of minor faults that the process's waited-for children have // made. CMinFlt uint // The number of major faults the process has made which have required // loading a memory page from disk. MajFlt uint // The number of major faults that the process's waited-for children have // made. CMajFlt uint // Amount of time that this process has been scheduled in user mode, // measured in clock ticks. UTime uint // Amount of time that this process has been scheduled in kernel mode, // measured in clock ticks. STime uint // Amount of time that this process's waited-for children have been // scheduled in user mode, measured in clock ticks. CUTime uint // Amount of time that this process's waited-for children have been // scheduled in kernel mode, measured in clock ticks. CSTime uint // For processes running a real-time scheduling policy, this is the negated // scheduling priority, minus one. Priority int // The nice value, a value in the range 19 (low priority) to -20 (high // priority). Nice int // Number of threads in this process. NumThreads int // The time the process started after system boot, the value is expressed // in clock ticks. Starttime uint64 // Virtual memory size in bytes. VSize int // Resident set size in pages. RSS int fs FS } // NewStat returns the current status information of the process. func (p Proc) NewStat() (ProcStat, error) { f, err := p.open("stat") if err != nil { return ProcStat{}, err } defer f.Close() data, err := ioutil.ReadAll(f) if err != nil { return ProcStat{}, err } var ( ignore int s = ProcStat{PID: p.PID, fs: p.fs} l = bytes.Index(data, []byte("(")) r = bytes.LastIndex(data, []byte(")")) ) if l < 0 || r < 0 { return ProcStat{}, fmt.Errorf( "unexpected format, couldn't extract comm: %s", data, ) } s.Comm = string(data[l+1 : r]) _, err = fmt.Fscan( bytes.NewBuffer(data[r+2:]), &s.State, &s.PPID, &s.PGRP, &s.Session, &s.TTY, &s.TPGID, &s.Flags, &s.MinFlt, &s.CMinFlt, &s.MajFlt, &s.CMajFlt, &s.UTime, &s.STime, &s.CUTime, &s.CSTime, &s.Priority, &s.Nice, &s.NumThreads, &ignore, &s.Starttime, &s.VSize, &s.RSS, ) if err != nil { return ProcStat{}, err } return s, nil } // VirtualMemory returns the virtual memory size in bytes. func (s ProcStat) VirtualMemory() int { return s.VSize } // ResidentMemory returns the resident memory size in bytes. func (s ProcStat) ResidentMemory() int { return s.RSS * os.Getpagesize() } // StartTime returns the unix timestamp of the process in seconds. func (s ProcStat) StartTime() (float64, error) { stat, err := s.fs.NewStat() if err != nil { return 0, err } return float64(stat.BootTime) + (float64(s.Starttime) / userHZ), nil } // CPUTime returns the total CPU user and system time in seconds. func (s ProcStat) CPUTime() float64 { return float64(s.UTime+s.STime) / userHZ } golang-procfs-0+git20150616.c91d8ee/proc_stat_test.go000066400000000000000000000043271254003330700221100ustar00rootroot00000000000000package procfs import "testing" func TestProcStat(t *testing.T) { fs, err := NewFS("fixtures") if err != nil { t.Fatal(err) } p, err := fs.NewProc(26231) if err != nil { t.Fatal(err) } s, err := p.NewStat() if err != nil { t.Fatal(err) } for _, test := range []struct { name string want int got int }{ {name: "pid", want: 26231, got: s.PID}, {name: "user time", want: 1677, got: int(s.UTime)}, {name: "system time", want: 44, got: int(s.STime)}, {name: "start time", want: 82375, got: int(s.Starttime)}, {name: "virtual memory size", want: 56274944, got: s.VSize}, {name: "resident set size", want: 1981, got: s.RSS}, } { if test.want != test.got { t.Errorf("want %s %d, got %d", test.name, test.want, test.got) } } } func TestProcStatComm(t *testing.T) { s1, err := testProcStat(26231) if err != nil { t.Fatal(err) } if want, got := "vim", s1.Comm; want != got { t.Errorf("want comm %s, got %s", want, got) } s2, err := testProcStat(584) if err != nil { t.Fatal(err) } if want, got := "(a b ) ( c d) ", s2.Comm; want != got { t.Errorf("want comm %s, got %s", want, got) } } func TestProcStatVirtualMemory(t *testing.T) { s, err := testProcStat(26231) if err != nil { t.Fatal(err) } if want, got := 56274944, s.VirtualMemory(); want != got { t.Errorf("want virtual memory %d, got %d", want, got) } } func TestProcStatResidentMemory(t *testing.T) { s, err := testProcStat(26231) if err != nil { t.Fatal(err) } if want, got := 1981*4096, s.ResidentMemory(); want != got { t.Errorf("want resident memory %d, got %d", want, got) } } func TestProcStatStartTime(t *testing.T) { s, err := testProcStat(26231) if err != nil { t.Fatal(err) } time, err := s.StartTime() if err != nil { t.Fatal(err) } if want, got := 1418184099.75, time; want != got { t.Errorf("want start time %f, got %f", want, got) } } func TestProcStatCPUTime(t *testing.T) { s, err := testProcStat(26231) if err != nil { t.Fatal(err) } if want, got := 17.21, s.CPUTime(); want != got { t.Errorf("want cpu time %f, got %f", want, got) } } func testProcStat(pid int) (ProcStat, error) { p, err := testProcess(pid) if err != nil { return ProcStat{}, err } return p.NewStat() } golang-procfs-0+git20150616.c91d8ee/proc_test.go000066400000000000000000000072431254003330700210550ustar00rootroot00000000000000package procfs import ( "os" "reflect" "sort" "testing" ) func TestSelf(t *testing.T) { p1, err := NewProc(os.Getpid()) if err != nil { t.Fatal(err) } p2, err := Self() if err != nil { t.Fatal(err) } if !reflect.DeepEqual(p1, p2) { t.Errorf("want process %v to equal %v", p1, p2) } } func TestAllProcs(t *testing.T) { fs, err := NewFS("fixtures") if err != nil { t.Fatal(err) } procs, err := fs.AllProcs() if err != nil { t.Fatal(err) } sort.Sort(procs) for i, p := range []*Proc{{PID: 584}, {PID: 26231}} { if want, got := p.PID, procs[i].PID; want != got { t.Errorf("want processes %d, got %d", want, got) } } } func TestCmdLine(t *testing.T) { for _, tt := range []struct { process int want []string }{ {process: 26231, want: []string{"vim", "test.go", "+10"}}, {process: 26232, want: []string{}}, } { p1, err := testProcess(tt.process) if err != nil { t.Fatal(err) } c1, err := p1.CmdLine() if err != nil { t.Fatal(err) } if !reflect.DeepEqual(tt.want, c1) { t.Errorf("want cmdline %v, got %v", tt.want, c1) } } } func TestExecutable(t *testing.T) { for _, tt := range []struct { process int want string }{ {process: 26231, want: "/usr/bin/vim"}, {process: 26232, want: ""}, } { p, err := testProcess(tt.process) if err != nil { t.Fatal(err) } exe, err := p.Executable() if err != nil { t.Fatal(err) } if !reflect.DeepEqual(tt.want, exe) { t.Errorf("want absolute path to cmdline %v, got %v", tt.want, exe) } } } func TestFileDescriptors(t *testing.T) { p1, err := testProcess(26231) if err != nil { t.Fatal(err) } fds, err := p1.FileDescriptors() if err != nil { t.Fatal(err) } sort.Sort(byUintptr(fds)) if want := []uintptr{0, 1, 2, 3, 10}; !reflect.DeepEqual(want, fds) { t.Errorf("want fds %v, got %v", want, fds) } p2, err := Self() if err != nil { t.Fatal(err) } fdsBefore, err := p2.FileDescriptors() if err != nil { t.Fatal(err) } s, err := os.Open("fixtures") if err != nil { t.Fatal(err) } defer s.Close() fdsAfter, err := p2.FileDescriptors() if err != nil { t.Fatal(err) } if len(fdsBefore)+1 != len(fdsAfter) { t.Errorf("want fds %v+1 to equal %v", fdsBefore, fdsAfter) } } func TestFileDescriptorTargets(t *testing.T) { p1, err := testProcess(26231) if err != nil { t.Fatal(err) } fds, err := p1.FileDescriptorTargets() if err != nil { t.Fatal(err) } sort.Strings(fds) var want = []string{ "../../symlinktargets/abc", "../../symlinktargets/def", "../../symlinktargets/ghi", "../../symlinktargets/uvw", "../../symlinktargets/xyz", } if !reflect.DeepEqual(want, fds) { t.Errorf("want fds %v, got %v", want, fds) } p2, err := Self() if err != nil { t.Fatal(err) } fdsBefore, err := p2.FileDescriptors() if err != nil { t.Fatal(err) } s, err := os.Open("fixtures") if err != nil { t.Fatal(err) } defer s.Close() fdsAfter, err := p2.FileDescriptors() if err != nil { t.Fatal(err) } if len(fdsBefore)+1 != len(fdsAfter) { t.Errorf("want fds %v+1 to equal %v", fdsBefore, fdsAfter) } } func TestFileDescriptorsLen(t *testing.T) { p1, err := testProcess(26231) if err != nil { t.Fatal(err) } l, err := p1.FileDescriptorsLen() if err != nil { t.Fatal(err) } if want, got := 5, l; want != got { t.Errorf("want fds %d, got %d", want, got) } } func testProcess(pid int) (Proc, error) { fs, err := NewFS("fixtures") if err != nil { return Proc{}, err } return fs.NewProc(pid) } type byUintptr []uintptr func (a byUintptr) Len() int { return len(a) } func (a byUintptr) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a byUintptr) Less(i, j int) bool { return a[i] < a[j] } golang-procfs-0+git20150616.c91d8ee/stat.go000066400000000000000000000022561254003330700200250ustar00rootroot00000000000000package procfs import ( "bufio" "fmt" "strconv" "strings" ) // Stat represents kernel/system statistics. type Stat struct { // Boot time in seconds since the Epoch. BootTime int64 } // NewStat returns kernel/system statistics read from /proc/stat. func NewStat() (Stat, error) { fs, err := NewFS(DefaultMountPoint) if err != nil { return Stat{}, err } return fs.NewStat() } // NewStat returns an information about current kernel/system statistics. func (fs FS) NewStat() (Stat, error) { f, err := fs.open("stat") if err != nil { return Stat{}, err } defer f.Close() s := bufio.NewScanner(f) for s.Scan() { line := s.Text() if !strings.HasPrefix(line, "btime") { continue } fields := strings.Fields(line) if len(fields) != 2 { return Stat{}, fmt.Errorf("couldn't parse %s line %s", f.Name(), line) } i, err := strconv.ParseInt(fields[1], 10, 32) if err != nil { return Stat{}, fmt.Errorf("couldn't parse %s: %s", fields[1], err) } return Stat{BootTime: i}, nil } if err := s.Err(); err != nil { return Stat{}, fmt.Errorf("couldn't parse %s: %s", f.Name(), err) } return Stat{}, fmt.Errorf("couldn't parse %s, missing btime", f.Name()) } golang-procfs-0+git20150616.c91d8ee/stat_test.go000066400000000000000000000004641254003330700210630ustar00rootroot00000000000000package procfs import "testing" func TestStat(t *testing.T) { fs, err := NewFS("fixtures") if err != nil { t.Fatal(err) } s, err := fs.NewStat() if err != nil { t.Fatal(err) } if want, got := int64(1418183276), s.BootTime; want != got { t.Errorf("want boot time %d, got %d", want, got) } }