├── .env ├── .gitignore ├── .travis.yml ├── CHANGELOG ├── HOW_TO_RELEASE.md ├── LICENSE.txt ├── README ├── README.md ├── bin └── graphitesend ├── debian ├── changelog ├── compat ├── control ├── copyright ├── files ├── rules └── source │ └── format ├── docs ├── Makefile ├── conf.py ├── index.rst ├── intro.rst ├── moduledocs.rst └── tutorial.rst ├── examples ├── proc_loadavg.py ├── proc_memcache.py └── proc_net_netstat.py ├── graphitesend ├── __init__.py ├── formatter.py └── graphitesend.py ├── requirements-asynchronous.txt ├── requirements-test.txt ├── requirements.txt ├── setup.py ├── tests ├── test_all.py ├── test_async.py ├── test_autoreconnect.py ├── test_cli.py ├── test_dryrun.py └── test_housekeeping.py └── tox.ini /.env: -------------------------------------------------------------------------------- 1 | use_env graphitesend 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.py[cod] 2 | 3 | # C extensions 4 | *.so 5 | 6 | # Packages 7 | *.egg 8 | *.egg-info 9 | dist 10 | build 11 | eggs 12 | parts 13 | var 14 | sdist 15 | develop-eggs 16 | .installed.cfg 17 | lib 18 | lib64 19 | 20 | # Installer logs 21 | pip-log.txt 22 | MANIFEST 23 | 24 | # Unit test / coverage reports 25 | .coverage 26 | .tox 27 | nosetests.xml 28 | 29 | # Translations 30 | *.mo 31 | 32 | # Mr Developer 33 | .mr.developer.cfg 34 | .project 35 | .pydevproject 36 | 37 | *.swp 38 | *.un~ 39 | *~* 40 | *#* 41 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | 3 | python: 4 | - "2.6" 5 | - "2.7" 6 | - "3.5" 7 | 8 | # command to install dependencies 9 | install: 10 | - "pip install -r requirements-test.txt" 11 | - "pip install gevent==1.1.0" 12 | 13 | # command to run tests 14 | script: 15 | - "flake8 --exclude=.tox,.virtualenv --ignore=E501" 16 | - "nosetests --with-coverage --cover-package=graphitesend" 17 | 18 | after_success: 19 | coveralls 20 | 21 | sudo: false 22 | cache: pip 23 | -------------------------------------------------------------------------------- /CHANGELOG: -------------------------------------------------------------------------------- 1 | 0.10.0 2 | ------ 3 | 4 | * Alexandre Bonnetain: Merge pull request #75 from daniellawrence/feature/integer-metric-name 5 | * Alexandre Bonnetain: Handle integer metric name 6 | * Alexandre Bonnetain: Merge pull request #73 from daniellawrence/formatter-file 7 | * Alexandre Bonnetain: Move the formatter's code to its own file 8 | * Alexandre Bonnetain: Merge pull request #72 from daniellawrence/fix/license-2017 9 | * Alexandre Bonnetain: Set gevent to 1.1.0 for Python 2.6 10 | * Alexandre Bonnetain: Update license file for 2017 11 | * Alexandre Bonnetain: Simpler formatting code (#69) 12 | * Dmitry S. Vlasov: add exponential backoff logic for autoreconnect (#66) 13 | * Daniel Lawrence: minor 14 | * Chris Carr: os.uname not supported on windows, use platform.uname 15 | * Alexandre Bonnetain: Merge branch 'factor_formatter' of https://github.com/zillow/graphitesend into master 16 | * Daniel: lock flake8 - due to python2.6 support (#68) 17 | * Daniel: adding py35 test runner (#63) 18 | * Jonathan Ultis: change whitespace in comments to satisfy flake8 19 | * Jonathan Ultis: remove redundant timestamp set 20 | * Jonathan Ultis: remove the untested simple graphite formatter to avoid annoying coverall 21 | * Jonathan Ultis: fix flakes 22 | * Jonathan Ultis: Factor the formatter out of the GraphiteClient. 23 | * kubauk: Fixing issue for Python3. Dispatch method uses methods that attempt to encode a bytes object when it had already. (#60) 24 | * Nate Tangsurat: fix: Init can use relative imports (#54) 25 | * Daniel: Merge pull request #58 from daniellawrence/fix/remove_monkeypatch 26 | * Daniel Lawrence: getting fix/remove_monkeypatch ready 27 | * Alexandre Bonnetain: Merge pull request #56 from daniellawrence/fix/test_clean_metric_name 28 | * Alexandre Bonnetain: Add tests for the case where clean_metric_name is set to false 29 | * Daniel: Merge pull request #55 from e4r7hbug/fix/tox 30 | * Nate Tangsurat: fix: Change asynchronous dependencies for tox 31 | * Alexandre Bonnetain: Pythonic adjustement 32 | * Alexandre Bonnetain: Monkey patch during test execution because it's not in the library anymore 33 | * Alexandre Bonnetain: Enforce the gevent monkey patching if the user activate asynchronoucity 34 | * Alexandre Bonnetain: Warn the user that he should monkey patch sockets to use asynchronoucity 35 | * Alexandre Bonnetain: Remove monkey patching 36 | 37 | 0.7.0 38 | ----- 39 | * Added python2.6 support, minor formatting change + unitest2 40 | * Added send message again when socket is empty (@e4r7hug) 41 | 42 | 0.6.1 43 | ----- 44 | * Added optional clean_metric_name (@Shir0kamii) 45 | 46 | 0.6.0 47 | ----- 48 | * Happy new year 49 | * Added autoreconnect (Thanks @Shir0kamii) 50 | 51 | 0.5.0 52 | ----- 53 | * Added asynchronous support via gevent (default: False) 54 | * Added timeout_in_seconds (default: 2) 55 | * Better test coverage 56 | * Minor PEP8 fixes 57 | 58 | 0.4.0 59 | ----- 60 | * Switched to tox for testing 61 | * Switched to a socket server for testing 62 | * Adding coverage test 63 | * Added coveralls.io link 64 | * Few pep8/flake fixes 65 | * Fixed missing pickle client 66 | 67 | 0.3.5 68 | ----- 69 | 70 | * Added CHANGELOG 71 | * Purge generated debian/* files from git 72 | -------------------------------------------------------------------------------- /HOW_TO_RELEASE.md: -------------------------------------------------------------------------------- 1 | How to release 2 | ----------------- 3 | 4 | * Update CHANGELOG 5 | * Update VERSION in graphitesend/graphitesend.py 6 | * Update version in setup.py 7 | 8 | 9 | git checkout -b release/x.y.z 10 | git commit CHANGELOG graphitesend/graphitesend.py setup.py 11 | git tag -a 12 | git push --tags 13 | python setup.py sdist upload 14 | 15 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2018 Danny Lawrence 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | README.md -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | graphitesend 2 | ============ 3 | 4 | [![Build Status](https://travis-ci.org/daniellawrence/graphitesend.png?banch=master)](https://travis-ci.org/daniellawrence/graphitesend?branch=master) 5 | ![](https://img.shields.io/pypi/dm/graphitesend.svg) 6 | ![](https://img.shields.io/pypi/dw/graphitesend.svg) 7 | ![](https://img.shields.io/pypi/dd/graphitesend.svg) 8 | [![Changelog](http://allmychanges.com/p/python/graphitesend/badge/)](http://allmychanges.com/p/python/graphitesend/?utm_source=badge) 9 | [![Coverage Status](https://coveralls.io/repos/daniellawrence/graphitesend/badge.png)](https://coveralls.io/r/daniellawrence/graphitesend) 10 | 11 | Easy python bindings to write to Carbon ( Re-write of carbonclient). 12 | 13 | Warning 14 | ------- 15 | 16 | This project is no longer maintained. Lacking rights to publish new versions, a 17 | collaborator has forked the project and maintain it there: 18 | [graphitesender](https://github.com/Shir0kamii/graphitesender) 19 | 20 | 21 | Read The Docs 22 | ----------- 23 | [graphitesend.rtfd.org](http://graphitesend.readthedocs.org/en/latest/) 24 | 25 | 26 | Blog posts 27 | ----------- 28 | [dansysadm.com](http://dansysadm.com/blog/2015/12/29/graphite-send.html) 29 | 30 | Example Scripts 31 | ---------------- 32 | The github repo of [graphitesend-examples](https://github.com/daniellawrence/graphitesend-examples) 33 | has lots of examples using graphitesend to grab data from your local linux system. 34 | 35 | Installing 36 | ---------- 37 | 38 | *pip* 39 | 40 | ````sh 41 | $ pip install graphitesend 42 | ```` 43 | 44 | *easy_install* 45 | ````sh 46 | $ easy_install graphitesend 47 | ```` 48 | 49 | or 50 | 51 | *source* 52 | 53 | ````sh 54 | $ git clone git://github.com/daniellawrence/graphitesend.git 55 | $ cd graphitesend 56 | $ python ./setup.py install 57 | ```` 58 | 59 | Usage Example 60 | -------------- 61 | 62 | Very basic sending of a metric called metric with a value of 45 63 | 64 | ````python 65 | >>> import graphitesend 66 | >>> graphitesend.init() 67 | >>> graphitesend.send('metric', 45) 68 | >>> graphitesend.send('metric2', 55) 69 | ```` 70 | 71 | The above would send the following metric to graphite 72 | 73 | system.localhostname.metric 45 epoch-time-stamp 74 | system.localhostname.metric2 55 epoch-time-stamp 75 | 76 | 77 | Cleaning up the interface and using a group of cpu to alter the metric prefix 78 | 79 | ````python 80 | >>> import graphitesend 81 | >>> g = graphitesend.init(group='cpu') 82 | >>> g.send('metric', 45) 83 | >>> g.send('metric2', 55) 84 | ```` 85 | 86 | The above would send the following metric to graphite 87 | 88 | system.localhostname.cpu.metric 45 epoch-time-stamp 89 | system.localhostname.cpu.metric2 55 epoch-time-stamp 90 | 91 | 92 | Using a different prefix (other then system.hostname) 93 | 94 | ````python 95 | >>> import graphitesend 96 | >>> g = graphitesend.init(prefix='apache.rc') 97 | >>> g.send('404', 4) 98 | >>> g.send('200', 500) 99 | ```` 100 | 101 | The above would send the following metric to graphite 102 | 103 | apache.rc.localhostname.404 4 epoch-time-stamp 104 | apache.rc.localhostname.200 500 epoch-time-stamp 105 | 106 | To get rid of `localhostname`, set another argument — `system_name` 107 | 108 | ````python 109 | >>> import graphitesend 110 | >>> g = graphitesend.init(prefix='apache.rc', system_name='') 111 | >>> g.send('404', 4) 112 | >>> g.send('200', 500) 113 | ```` 114 | 115 | The above would send the following metric to graphite 116 | 117 | apache.rc.404 4 epoch-time-stamp 118 | apache.rc.200 500 epoch-time-stamp 119 | 120 | 121 | Sending a dict() 122 | 123 | ````python 124 | >>> import graphitesend 125 | >>> g = graphitesend.init() 126 | >>> g.send_dict({'metric': 45, 'metric2': 55}) 127 | ```` 128 | 129 | Sending a list() 130 | 131 | ````python 132 | >>> import graphitesend 133 | >>> g = graphitesend.init() 134 | >>> g.send_list([('metric', 45), ('metric2', 55)]) 135 | ```` 136 | 137 | Sending a list(), with a custom timestamp for all metric-value pairs 138 | 139 | ````python 140 | >>> import graphitesend 141 | >>> g = graphitesend.init() 142 | >>> g.send_list([('metric', 45), ('metric2', 55)], timestamp=12345) 143 | ```` 144 | 145 | Sending a list(), with a custom timestamp for each metric-value pairs 146 | 147 | ````python 148 | >>> import graphitesend 149 | >>> g = graphitesend.init() 150 | >>> g.send_list([('metric', 45, 1234), ('metric2', 55, 1234)]) 151 | ```` 152 | 153 | Learning? Use dryrun. 154 | ---------------------- 155 | 156 | With dryrun enabled the data will never get sent to a remote service, it will 157 | just print out what would have been sent. 158 | 159 | ````python 160 | >>> import graphitesend 161 | >>> g = graphitesend.init(dryrun=True) 162 | >>> g.send_list([('metric', 45, 1234), ('metric2', 55, 1234)]) 163 | ```` 164 | 165 | Example: the init() 166 | ---------------- 167 | 168 | Set a metric prefix (Default arg) 169 | ````python 170 | >>> g = graphitesend.init('prefix') 171 | >>> print g.send('metric', 1) 172 | sent 34 long message: prefix.metric 1.000000 1365068929 173 | ```` 174 | 175 | set a metric prefix using kwargs 176 | ````python 177 | >>> g = graphitesend.init(prefix='prefix') 178 | >>> print g.send('metric', 2) 179 | sent 34 long message: prefix.metric 2.000000 1365068929 180 | ```` 181 | 182 | Squash any dots in the hostname 183 | ```` 184 | >>> g = graphitesend.init(fqdn_squash=True) 185 | >>> print g.send('metric', 3) 186 | sent 56 long message: systems.host_example_com.metric 2.00000 1365069029 187 | ```` 188 | 189 | view the default prefix, hardset systems. then followed by the name of the 190 | host that execute the send(). 191 | ````python 192 | >>> g = graphitesend.init() 193 | >>> print g.send('metric', 3) 194 | sent 44 long message: systems..metric 3.000000 1365069029 195 | ```` 196 | 197 | Set a suffix, handy if you have a bunch of timers or percentages 198 | ````python 199 | >>> g = graphitesend.init(suffix='_ms') 200 | >>> print g.send('metric', 4) 201 | sent 47 long message: systems..metric_ms 4.000000 1365069100 202 | ```` 203 | 204 | set a system_name if your submitting results for a different system 205 | ````python 206 | >>> g = graphitesend.init(system_name='othersystem') 207 | >>> print g.send('metric', 5) 208 | sent 47 long message: systems.othersystem.metric 5.000000 1365069100 209 | ```` 210 | 211 | Lowercase all the metric names that are send to the graphite server. 212 | ````python 213 | >>> g = graphitesend.init(lowercase_metric_names=True) 214 | >>> print g.send('METRIC', 6) 215 | sent 47 long message: systems..metric 6.000000 1365069100 216 | ```` 217 | 218 | 219 | Set a group name, handy if you just parsed iostat and want to prefix all the 220 | metrics with iostat, after its already in the directory. 221 | ````python 222 | >>> g = graphitesend.init(group='groupname') 223 | >>> print g.send('metric', 6) 224 | sent 54 long message: systems..groupname.metric 6.000000 136506924 225 | ```` 226 | 227 | Connect to a different graphite server 228 | ````python 229 | >>> graphitesend.init(graphite_server='graphite.example.com') 230 | ```` 231 | 232 | Connect to a different graphite server port 233 | ````python 234 | >>> graphitesend.init(graphite_port=2003) 235 | ```` 236 | 237 | 238 | Send async messages 239 | ````python 240 | >>> graphitesend.init(asynchronous=True) 241 | ```` 242 | 243 | 244 | Change connect timeout (default 2) 245 | ````python 246 | >>> graphitesend.init(timeout_in_seconds=5) 247 | ```` 248 | 249 | 250 | CLI 251 | ------------ 252 | 253 | Just added -- A cli script that allows for anything to send metrics over to 254 | graphite (not just python). 255 | 256 | The usage is very simple you need to give the command a metric and a value. 257 | 258 | ````sh 259 | $ graphitesend name.of.the.metric 666 260 | ```` 261 | 262 | Send more\* then 1 metric and value 263 | 264 | ````sh 265 | $ graphitesend name.of.the.metric 666 266 | $ graphitesend name.of.the.other_metric 2 267 | ```` 268 | 269 | \* Call it 2 times ;) 270 | 271 | 272 | 273 | Porcelain Overview 274 | ================== 275 | 276 | init 277 | ----- 278 | Create the module instance of GraphiteSend. 279 | 280 | send 281 | ----- 282 | Make sure that we have an instance of the GraphiteClient. 283 | Then send the metrics to the graphite server. 284 | 285 | send_dict 286 | --------- 287 | Make sure that we have an instance of the GraphiteClient. 288 | Then send the metrics to the graphite server. 289 | 290 | reset 291 | ----- 292 | Disconnect from the graphite server and destroy the module instance. 293 | 294 | 295 | TCP vs UDP 296 | ========== 297 | 298 | There is a new branch for UDP support called 'udp and tcp'. 299 | TCP will continue to be the default with UDP as an option 300 | -------------------------------------------------------------------------------- /bin/graphitesend: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import graphitesend 3 | graphitesend.cli() 4 | -------------------------------------------------------------------------------- /debian/changelog: -------------------------------------------------------------------------------- 1 | graphitesend (0.4.0-1) UNRELEASED; urgency=low 2 | 3 | * Initial release. (Closes: #XXXXXX) 4 | 5 | -- Danny Lawrence Sat, 06 Apr 2013 16:25:55 +1100 6 | -------------------------------------------------------------------------------- /debian/compat: -------------------------------------------------------------------------------- 1 | 8 2 | -------------------------------------------------------------------------------- /debian/control: -------------------------------------------------------------------------------- 1 | Source: graphitesend 2 | Maintainer: Danny Lawrence 3 | Section: misc 4 | Priority: optional 5 | Standards-Version: 3.9.2 6 | Build-Depends: debhelper (>= 8) 7 | 8 | Package: graphitesend 9 | Architecture: all 10 | Depends: ${shlibs:Depends}, ${misc:Depends} 11 | Description: send metrics to graphite 12 | Python library that makes it easy to graphite. 13 | -------------------------------------------------------------------------------- /debian/copyright: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daniellawrence/graphitesend/02281263e642f9b6e146886d4544e1d7aebd7753/debian/copyright -------------------------------------------------------------------------------- /debian/files: -------------------------------------------------------------------------------- 1 | graphitesend_0.0.4-1_all.deb misc optional 2 | -------------------------------------------------------------------------------- /debian/rules: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | %: 3 | dh $@ 4 | -------------------------------------------------------------------------------- /debian/source/format: -------------------------------------------------------------------------------- 1 | 3.0 (quilt) 2 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = _build 9 | 10 | # User-friendly check for sphinx-build 11 | ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) 12 | $(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) 13 | endif 14 | 15 | # Internal variables. 16 | PAPEROPT_a4 = -D latex_paper_size=a4 17 | PAPEROPT_letter = -D latex_paper_size=letter 18 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 19 | # the i18n builder cannot share the environment and doctrees with the others 20 | I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 21 | 22 | .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext 23 | 24 | help: 25 | @echo "Please use \`make ' where is one of" 26 | @echo " html to make standalone HTML files" 27 | @echo " dirhtml to make HTML files named index.html in directories" 28 | @echo " singlehtml to make a single large HTML file" 29 | @echo " pickle to make pickle files" 30 | @echo " json to make JSON files" 31 | @echo " htmlhelp to make HTML files and a HTML help project" 32 | @echo " qthelp to make HTML files and a qthelp project" 33 | @echo " devhelp to make HTML files and a Devhelp project" 34 | @echo " epub to make an epub" 35 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 36 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 37 | @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" 38 | @echo " text to make text files" 39 | @echo " man to make manual pages" 40 | @echo " texinfo to make Texinfo files" 41 | @echo " info to make Texinfo files and run them through makeinfo" 42 | @echo " gettext to make PO message catalogs" 43 | @echo " changes to make an overview of all changed/added/deprecated items" 44 | @echo " xml to make Docutils-native XML files" 45 | @echo " pseudoxml to make pseudoxml-XML files for display purposes" 46 | @echo " linkcheck to check all external links for integrity" 47 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 48 | 49 | clean: 50 | rm -rf $(BUILDDIR)/* 51 | 52 | html: 53 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 54 | @echo 55 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 56 | 57 | dirhtml: 58 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 59 | @echo 60 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 61 | 62 | singlehtml: 63 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 64 | @echo 65 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 66 | 67 | pickle: 68 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 69 | @echo 70 | @echo "Build finished; now you can process the pickle files." 71 | 72 | json: 73 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 74 | @echo 75 | @echo "Build finished; now you can process the JSON files." 76 | 77 | htmlhelp: 78 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 79 | @echo 80 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 81 | ".hhp project file in $(BUILDDIR)/htmlhelp." 82 | 83 | qthelp: 84 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 85 | @echo 86 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 87 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 88 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/graphitesend.qhcp" 89 | @echo "To view the help file:" 90 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/graphitesend.qhc" 91 | 92 | devhelp: 93 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 94 | @echo 95 | @echo "Build finished." 96 | @echo "To view the help file:" 97 | @echo "# mkdir -p $$HOME/.local/share/devhelp/graphitesend" 98 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/graphitesend" 99 | @echo "# devhelp" 100 | 101 | epub: 102 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 103 | @echo 104 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 105 | 106 | latex: 107 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 108 | @echo 109 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 110 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 111 | "(use \`make latexpdf' here to do that automatically)." 112 | 113 | latexpdf: 114 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 115 | @echo "Running LaTeX files through pdflatex..." 116 | $(MAKE) -C $(BUILDDIR)/latex all-pdf 117 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 118 | 119 | latexpdfja: 120 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 121 | @echo "Running LaTeX files through platex and dvipdfmx..." 122 | $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja 123 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 124 | 125 | text: 126 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 127 | @echo 128 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 129 | 130 | man: 131 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 132 | @echo 133 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 134 | 135 | texinfo: 136 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 137 | @echo 138 | @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." 139 | @echo "Run \`make' in that directory to run these through makeinfo" \ 140 | "(use \`make info' here to do that automatically)." 141 | 142 | info: 143 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 144 | @echo "Running Texinfo files through makeinfo..." 145 | make -C $(BUILDDIR)/texinfo info 146 | @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." 147 | 148 | gettext: 149 | $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale 150 | @echo 151 | @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." 152 | 153 | changes: 154 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 155 | @echo 156 | @echo "The overview file is in $(BUILDDIR)/changes." 157 | 158 | linkcheck: 159 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 160 | @echo 161 | @echo "Link check complete; look for any errors in the above output " \ 162 | "or in $(BUILDDIR)/linkcheck/output.txt." 163 | 164 | doctest: 165 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 166 | @echo "Testing of doctests in the sources finished, look at the " \ 167 | "results in $(BUILDDIR)/doctest/output.txt." 168 | 169 | xml: 170 | $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml 171 | @echo 172 | @echo "Build finished. The XML files are in $(BUILDDIR)/xml." 173 | 174 | pseudoxml: 175 | $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml 176 | @echo 177 | @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." 178 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # graphitesend documentation build configuration file, created by 4 | # sphinx-quickstart on Thu Nov 14 18:45:17 2013. 5 | # 6 | # This file is execfile()d with the current directory set to its 7 | # containing dir. 8 | # 9 | # Note that not all possible configuration values are present in this 10 | # autogenerated file. 11 | # 12 | # All configuration values have a default; values that are commented out 13 | # serve to show the default. 14 | 15 | import sys 16 | import os 17 | 18 | # If extensions (or modules to document with autodoc) are in another directory, 19 | # add these directories to sys.path here. If the directory is relative to the 20 | # documentation root, use os.path.abspath to make it absolute, like shown here. 21 | # sys.path.insert(0, os.path.abspath('.')) 22 | sys.path.insert(0, os.path.abspath(os.path.join(os.getcwd(), '..'))) 23 | 24 | # -- General configuration ------------------------------------------------ 25 | 26 | # If your documentation needs a minimal Sphinx version, state it here. 27 | # needs_sphinx = '1.0' 28 | 29 | # Add any Sphinx extension module names here, as strings. They can be 30 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom 31 | # ones. 32 | extensions = [ 33 | 'sphinx.ext.autodoc', 34 | 'sphinx.ext.coverage', 35 | 'sphinx.ext.viewcode', 36 | ] 37 | 38 | # Add any paths that contain templates here, relative to this directory. 39 | templates_path = ['_templates'] 40 | 41 | # The suffix of source filenames. 42 | source_suffix = '.rst' 43 | 44 | # The encoding of source files. 45 | # source_encoding = 'utf-8-sig' 46 | 47 | # The master toctree document. 48 | master_doc = 'index' 49 | 50 | # General information about the project. 51 | project = u'graphitesend' 52 | copyright = u'2013, Daniel Lawrence ' 53 | 54 | # The version info for the project you're documenting, acts as replacement for 55 | # |version| and |release|, also used in various other places throughout the 56 | # built documents. 57 | # 58 | # The short X.Y version. 59 | version = '0.2.0' 60 | # The full version, including alpha/beta/rc tags. 61 | release = '0.2.0' 62 | 63 | # The language for content autogenerated by Sphinx. Refer to documentation 64 | # for a list of supported languages. 65 | # language = None 66 | 67 | # There are two options for replacing |today|: either, you set today to some 68 | # non-false value, then it is used: 69 | # today = '' 70 | # Else, today_fmt is used as the format for a strftime call. 71 | # today_fmt = '%B %d, %Y' 72 | 73 | # List of patterns, relative to source directory, that match files and 74 | # directories to ignore when looking for source files. 75 | exclude_patterns = ['_build'] 76 | 77 | # The reST default role (used for this markup: `text`) to use for all 78 | # documents. 79 | # default_role = None 80 | 81 | # If true, '()' will be appended to :func: etc. cross-reference text. 82 | # add_function_parentheses = True 83 | 84 | # If true, the current module name will be prepended to all description 85 | # unit titles (such as .. function::). 86 | # add_module_names = True 87 | 88 | # If true, sectionauthor and moduleauthor directives will be shown in the 89 | # output. They are ignored by default. 90 | # show_authors = False 91 | 92 | # The name of the Pygments (syntax highlighting) style to use. 93 | pygments_style = 'sphinx' 94 | 95 | # A list of ignored prefixes for module index sorting. 96 | # modindex_common_prefix = [] 97 | 98 | # If true, keep warnings as "system message" paragraphs in the built documents. 99 | # keep_warnings = False 100 | 101 | 102 | # -- Options for HTML output ---------------------------------------------- 103 | 104 | # The theme to use for HTML and HTML Help pages. See the documentation for 105 | # a list of builtin themes. 106 | html_theme = 'default' 107 | 108 | # Theme options are theme-specific and customize the look and feel of a theme 109 | # further. For a list of options available for each theme, see the 110 | # documentation. 111 | # html_theme_options = {} 112 | 113 | # Add any paths that contain custom themes here, relative to this directory. 114 | # html_theme_path = [] 115 | 116 | # The name for this set of Sphinx documents. If None, it defaults to 117 | # " v documentation". 118 | # html_title = None 119 | 120 | # A shorter title for the navigation bar. Default is the same as html_title. 121 | # html_short_title = None 122 | 123 | # The name of an image file (relative to this directory) to place at the top 124 | # of the sidebar. 125 | # html_logo = None 126 | 127 | # The name of an image file (within the static path) to use as favicon of the 128 | # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 129 | # pixels large. 130 | # html_favicon = None 131 | 132 | # Add any paths that contain custom static files (such as style sheets) here, 133 | # relative to this directory. They are copied after the builtin static files, 134 | # so a file named "default.css" will overwrite the builtin "default.css". 135 | html_static_path = ['_static'] 136 | 137 | # Add any extra paths that contain custom files (such as robots.txt or 138 | # .htaccess) here, relative to this directory. These files are copied 139 | # directly to the root of the documentation. 140 | # html_extra_path = [] 141 | 142 | # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, 143 | # using the given strftime format. 144 | # html_last_updated_fmt = '%b %d, %Y' 145 | 146 | # If true, SmartyPants will be used to convert quotes and dashes to 147 | # typographically correct entities. 148 | # html_use_smartypants = True 149 | 150 | # Custom sidebar templates, maps document names to template names. 151 | # html_sidebars = {} 152 | 153 | # Additional templates that should be rendered to pages, maps page names to 154 | # template names. 155 | # html_additional_pages = {} 156 | 157 | # If false, no module index is generated. 158 | # html_domain_indices = True 159 | 160 | # If false, no index is generated. 161 | # html_use_index = True 162 | 163 | # If true, the index is split into individual pages for each letter. 164 | # html_split_index = False 165 | 166 | # If true, links to the reST sources are added to the pages. 167 | # html_show_sourcelink = True 168 | 169 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. 170 | # html_show_sphinx = True 171 | 172 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. 173 | # html_show_copyright = True 174 | 175 | # If true, an OpenSearch description file will be output, and all pages will 176 | # contain a tag referring to it. The value of this option must be the 177 | # base URL from which the finished HTML is served. 178 | # html_use_opensearch = '' 179 | 180 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 181 | # html_file_suffix = None 182 | 183 | # Output file base name for HTML help builder. 184 | htmlhelp_basename = 'graphitesenddoc' 185 | 186 | 187 | # -- Options for LaTeX output --------------------------------------------- 188 | 189 | latex_elements = { 190 | # The paper size ('letterpaper' or 'a4paper'). 191 | # 'papersize': 'letterpaper', 192 | 193 | # The font size ('10pt', '11pt' or '12pt'). 194 | # 'pointsize': '10pt', 195 | 196 | # Additional stuff for the LaTeX preamble. 197 | # 'preamble': '', 198 | } 199 | 200 | # Grouping the document tree into LaTeX files. List of tuples 201 | # (source start file, target name, title, 202 | # author, documentclass [howto, manual, or own class]). 203 | latex_documents = [ 204 | ('index', 'graphitesend.tex', u'graphitesend Documentation', 205 | u'Daniel Lawrence \\textless{}dannyla@linux.com\\textgreater{}', 'manual'), 206 | ] 207 | 208 | # The name of an image file (relative to this directory) to place at the top of 209 | # the title page. 210 | # latex_logo = None 211 | 212 | # For "manual" documents, if this is true, then toplevel headings are parts, 213 | # not chapters. 214 | # latex_use_parts = False 215 | 216 | # If true, show page references after internal links. 217 | # latex_show_pagerefs = False 218 | 219 | # If true, show URL addresses after external links. 220 | # latex_show_urls = False 221 | 222 | # Documents to append as an appendix to all manuals. 223 | # latex_appendices = [] 224 | 225 | # If false, no module index is generated. 226 | # latex_domain_indices = True 227 | 228 | 229 | # -- Options for manual page output --------------------------------------- 230 | 231 | # One entry per manual page. List of tuples 232 | # (source start file, name, description, authors, manual section). 233 | man_pages = [ 234 | ('index', 'graphitesend', u'graphitesend Documentation', 235 | [u'Daniel Lawrence '], 1) 236 | ] 237 | 238 | # If true, show URL addresses after external links. 239 | # man_show_urls = False 240 | 241 | 242 | # -- Options for Texinfo output ------------------------------------------- 243 | 244 | # Grouping the document tree into Texinfo files. List of tuples 245 | # (source start file, target name, title, author, 246 | # dir menu entry, description, category) 247 | texinfo_documents = [ 248 | ('index', 'graphitesend', u'graphitesend Documentation', 249 | u'Daniel Lawrence ', 'graphitesend', 'One line description of project.', 250 | 'Miscellaneous'), 251 | ] 252 | 253 | # Documents to append as an appendix to all manuals. 254 | # texinfo_appendices = [] 255 | 256 | # If false, no module index is generated. 257 | # texinfo_domain_indices = True 258 | 259 | # How to display URL addresses: 'footnote', 'no', or 'inline'. 260 | # texinfo_show_urls = 'footnote' 261 | 262 | # If true, do not generate a @detailmenu in the "Top" node's menu. 263 | # texinfo_no_detailmenu = False 264 | 265 | RTD_NEW_THEME = True 266 | -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | .. graphitesend documentation master file, created by 2 | sphinx-quickstart on Thu Nov 14 18:45:17 2013. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | 7 | ======================================== 8 | GraphiteSend 9 | ======================================== 10 | 11 | About 12 | ===== 13 | 14 | .. include:: intro.rst 15 | 16 | Installation 17 | ============ 18 | 19 | Stable releases of graphitsend are best installed via ``pip`` or ``easy_install``. 20 | 21 | We recommend using the latest stable version of graphitsend; releases are made often 22 | to prevent any large gaps in functionality between the latest stable release 23 | and the development version. 24 | 25 | However, if you want to live on the edge, you can pull down the source code 26 | from our Git repository, or fork us on Github. 27 | 28 | 29 | Documentation 30 | ============= 31 | 32 | .. toctree:: 33 | :maxdepth: 3 34 | 35 | intro 36 | tutorial 37 | moduledocs 38 | 39 | 40 | Bugs/ticket tracker 41 | ------------------- 42 | 43 | To file new bugs or search existing ones, you may visit GraphiteSends's `Github Issues 44 | `_ page. This does require a (free, easy to set up) Github account. 45 | 46 | -------------------------------------------------------------------------------- /docs/intro.rst: -------------------------------------------------------------------------------- 1 | Graphitesend is a python library that can be used to easily push data into graphite using python. 2 | 3 | 4 | .. graphviz:: 5 | 6 | digraph foo { 7 | "bar" -> "baz"; 8 | } 9 | 10 | 11 | Usage Example 12 | ------------- 13 | 14 | Very basic sending of a metric called metric with a value of 45 15 | 16 | >>> import graphitesend 17 | >>> graphitesend.init() 18 | >>> graphitesend.send('metric', 45) 19 | >>> graphitesend.send('metric2', 55) 20 | 21 | The above would send the following metric to graphite over the plaintext (default) protocol on port 2003 (default) 22 | 23 | :: 24 | 25 | system.localhostname.metric 45 epoch-time-stamp 26 | system.localhostname.metric2 55 epoch-time-stamp 27 | 28 | Cleaning up the interface and using a group of cpu to alter the metric prefix 29 | 30 | >>> import graphitesend 31 | >>> g = graphitesend.init(group='cpu') 32 | >>> g.send('metric', 45) 33 | >>> g.send('metric2', 55) 34 | 35 | The above would send the following metric to graphite 36 | 37 | :: 38 | 39 | system.localhostname.cpu.metric 45 epoch-time-stamp 40 | system.localhostname.cpu.metric2 55 epoch-time-stamp 41 | 42 | 43 | Using graphitesend from the commandline 44 | ======================================= 45 | 46 | A cli script that allows for anything to send metrics over to 47 | graphite (not just python). 48 | 49 | The usage is very simple you need to give the command a metric and a value. 50 | 51 | 52 | :: 53 | 54 | $ graphitesend name.of.the.metric 666 55 | 56 | Send more\* then 1 metric and value 57 | 58 | :: 59 | 60 | $ graphitesend name.of.the.metric 666 61 | $ graphitesend name.of.the.other_metric 2 62 | 63 | 64 | 65 | Example Scripts using graphitesend 66 | ================================== 67 | 68 | The github repo of https://github.com/daniellawrence/graphitesend-examples 69 | has lots of examples using graphitesend to grab data from your local linux system. 70 | -------------------------------------------------------------------------------- /docs/moduledocs.rst: -------------------------------------------------------------------------------- 1 | 2 | ================ 3 | GraphiteSend API 4 | ================ 5 | 6 | .. automodule:: graphitesend 7 | :members: 8 | -------------------------------------------------------------------------------- /docs/tutorial.rst: -------------------------------------------------------------------------------- 1 | ===================== 2 | Overview and Tutorial 3 | ===================== 4 | 5 | Welcome to GraphiteSend! 6 | 7 | This is a quick dive in at some of the features of graphitesend. 8 | 9 | What is GraphiteSend? 10 | ===================== 11 | 12 | As the ``README`` says: 13 | 14 | Graphitesend is a python library that can be used to easily push data into graphite using python. 15 | 16 | More specifically, Graphite is: 17 | 18 | * A common way for you to push all your metrics that your going to gather in 19 | python to your graphite server. 20 | 21 | The most common usage of this is to either 22 | 23 | - quickly put to gether a new script that is going to metrics into graphite. 24 | - extending an oldscript to standarize how to push metrics into graphite. 25 | 26 | 27 | Hello, ``graphite`` 28 | =================== 29 | 30 | Very basic sending of a metric called ``hello world`` with the current value of 53. 31 | 32 | This will make a connection to the a graphite server called (configurable) and pass the following 33 | 34 | >>> graphitesend.send('hello world', 45) 35 | systems.ubuntu.hello_world 45.000000 1386490491 36 | 37 | As you can see ``graphitesend`` has done a few things for you.. 38 | 39 | * Added a default ``prefix`` of "systems." to make sure all your metrics land in the same namespace 40 | * Added the ``system_name`` as the current hostname after the prefix. 41 | * Fixed the space in the metric name 42 | * validated and converted the value into a float 43 | * Used the current timestamp 44 | * Send all the above to the graphitesend on the plain text protocol, default port 2003 45 | 46 | Sending Dicts of data 47 | ===================== 48 | 49 | Instead of sending single metrics to the graphite server you can group them up into a ``dict`` or 50 | ``list``. 51 | 52 | >>> graphitesend.send_dict({'hello world': 45, 'goodbye world': 54}) 53 | systems.ubuntu.hello_world 45.000000 1386490491 54 | systems.ubuntu.goodbye_world 54.000000 1386490491 55 | 56 | As long as you keep the format of ``{metric: value}`` the data will be sent over to graphite. 57 | 58 | >>> graphitesend.send_dict( 59 | ... { 60 | ... 'hello world': 45, 61 | ... 'this world': 54 62 | ... 'goodbye world': 54 63 | ... } 64 | ... ) 65 | 66 | Sending lists of data 67 | ===================== 68 | 69 | You can do the same as sending dicts however by providing a list. 70 | 71 | >>> graphitesend.send_list([('hello world', 45), ('goodbye world', 54)) 72 | systems.ubuntu.hello_world 45.000000 1386490491 73 | systems.ubuntu.goodbye_world 54.000000 1386490491 74 | 75 | As long as you keep the format of ``metric, value, [timestamp]`` the data will be sent over to graphite. 76 | 77 | The optional timestamp needs to be provided in unix epoch format. 78 | 79 | >>> graphitesend.send_list( 80 | ... [ 81 | ... ('hello world', 45), 82 | ... ('this world', 54), 83 | ... ('goodbye world', 54, 10000) 84 | ... ] 85 | ... ) 86 | 87 | 88 | -------------------------------------------------------------------------------- /examples/proc_loadavg.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import time 3 | import graphitesend 4 | # comments 5 | g = graphitesend.init(group='loadavg_', suffix='min') 6 | 7 | while True: 8 | (la1, la5, la15) = open('/proc/loadavg').read().strip().split()[:3] 9 | print(g.send_dict({'1': la1, '5': la5, '15': la15})) 10 | time.sleep(1) 11 | -------------------------------------------------------------------------------- /examples/proc_memcache.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import graphitesend 3 | 4 | g = graphitesend.init(group='meminfo.', suffix='_mb', 5 | lowercase_metric_names=True) 6 | data = [] 7 | for line in open('/proc/meminfo').readlines(): 8 | bits = line.split() 9 | 10 | # We dont care about the pages. 11 | if len(bits) == 2: 12 | continue 13 | 14 | # remove the : from the metric name 15 | metric = bits[0] 16 | metric = metric.replace(':', '') 17 | 18 | # Covert the default kb into mb 19 | value = int(bits[1]) 20 | value = value / 1024 21 | 22 | data.append((metric, value)) 23 | 24 | print(g.send_list(data)) 25 | -------------------------------------------------------------------------------- /examples/proc_net_netstat.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import graphitesend 4 | 5 | lines = open('/proc/net/netstat').readlines() 6 | 7 | tcp_metrics = lines[0].split()[1:] 8 | tcp_values = lines[1].split()[1:] 9 | ip_metrics = lines[2].split()[1:] 10 | ip_values = lines[3].split()[1:] 11 | 12 | data_list = zip(tcp_metrics + ip_metrics, tcp_values + ip_values) 13 | 14 | g = graphitesend.init(group='netstat.', lowercase_metric_names=True) 15 | print(g.send_list(data_list)) 16 | -------------------------------------------------------------------------------- /graphitesend/__init__.py: -------------------------------------------------------------------------------- 1 | from .graphitesend import * # noqa 2 | -------------------------------------------------------------------------------- /graphitesend/formatter.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import platform 3 | import time 4 | 5 | log = logging.getLogger("graphitesend") 6 | 7 | 8 | class GraphiteStructuredFormatter(object): 9 | '''Default formatter for GraphiteClient. 10 | 11 | Provides structured metric naming based on a prefix, system name, group, etc 12 | 13 | :param prefix: string added to the start of all metrics 14 | :type prefix: Default: "systems." 15 | :param group: string added to after system_name and before metric name 16 | :param system_name: FDQN of the system generating the metrics 17 | :type system_name: Default: current FDQN 18 | :param suffix: string added to the end of all metrics 19 | :param lowercase_metric_names: Toggle the .lower() of all metric names 20 | :param fqdn_squash: Change host.example.com to host_example_com 21 | :type fqdn_squash: True or False 22 | :param clean_metric_name: Does GraphiteClient needs to clean metric's name 23 | :type clean_metric_name: True or False 24 | 25 | Feel free to implement your own formatter as any callable that accepts 26 | def __call__(metric_name, metric_value, timestamp) 27 | 28 | and emits text appropriate to send to graphite's text socket. 29 | ''' 30 | 31 | cleaning_replacement_list = [ 32 | ('(', '_'), 33 | (')', ''), 34 | (' ', '_'), 35 | ('-', '_'), 36 | ('/', '_'), 37 | ('\\', '_') 38 | ] 39 | 40 | def __init__(self, prefix=None, group=None, system_name=None, suffix=None, 41 | lowercase_metric_names=False, fqdn_squash=False, clean_metric_name=True): 42 | 43 | prefix_parts = [] 44 | 45 | if prefix != '': 46 | prefix = prefix or "systems" 47 | prefix_parts.append(prefix) 48 | 49 | if system_name != '': 50 | system_name = system_name or platform.uname()[1] 51 | if fqdn_squash: 52 | system_name = system_name.replace('.', '_') 53 | prefix_parts.append(system_name) 54 | 55 | if group is not None: 56 | prefix_parts.append(group) 57 | 58 | prefix = '.'.join(prefix_parts) 59 | prefix = prefix.replace('..', '.') # remove double dots 60 | prefix = prefix.replace(' ', '_') # Replace ' 'spaces with _ 61 | if prefix: 62 | prefix += '.' 63 | self.prefix = prefix 64 | 65 | self.suffix = suffix or "" 66 | self.lowercase_metric_names = lowercase_metric_names 67 | self._clean_metric_name = clean_metric_name 68 | 69 | def clean_metric_name(self, metric_name): 70 | """ 71 | Make sure the metric is free of control chars, spaces, tabs, etc. 72 | """ 73 | if not self._clean_metric_name: 74 | return metric_name 75 | metric_name = str(metric_name) 76 | for _from, _to in self.cleaning_replacement_list: 77 | metric_name = metric_name.replace(_from, _to) 78 | return metric_name 79 | 80 | '''Format a metric, value, and timestamp for use on the carbon text socket.''' 81 | def __call__(self, metric_name, metric_value, timestamp=None): 82 | if timestamp is None: 83 | timestamp = time.time() 84 | timestamp = int(timestamp) 85 | 86 | if type(metric_value).__name__ in ['str', 'unicode']: 87 | metric_value = float(metric_value) 88 | 89 | log.debug("metric: '%s'" % metric_name) 90 | metric_name = self.clean_metric_name(metric_name) 91 | log.debug("metric: '%s'" % metric_name) 92 | 93 | message = "%s%s%s %f %d\n" % (self.prefix, metric_name, self.suffix, 94 | metric_value, timestamp) 95 | 96 | # An option to lowercase the entire message 97 | if self.lowercase_metric_names: 98 | message = message.lower() 99 | 100 | return message 101 | -------------------------------------------------------------------------------- /graphitesend/graphitesend.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | try: 4 | import gevent 5 | except ImportError: 6 | gevent = False 7 | 8 | import pickle 9 | import socket 10 | import struct 11 | import time 12 | import random 13 | 14 | from .formatter import GraphiteStructuredFormatter 15 | 16 | _module_instance = None 17 | 18 | default_graphite_pickle_port = 2004 19 | default_graphite_plaintext_port = 2003 20 | default_graphite_server = 'graphite' 21 | 22 | VERSION = "0.10.0" 23 | 24 | 25 | class GraphiteSendException(Exception): 26 | pass 27 | 28 | 29 | class GraphiteClient(object): 30 | """ 31 | Graphite Client that will setup a TCP connection to graphite. 32 | 33 | :param prefix: string added to the start of all metrics 34 | :type prefix: Default: "systems." 35 | :param graphite_server: hostname or ip address of graphite server 36 | :type graphite_server: Default: graphite 37 | :param graphite_port: TCP port we will connect to 38 | :type graphite_port: Default: 2003 39 | :param debug: Toggle debug messages 40 | :type debug: True or False 41 | :param group: string added to after system_name and before metric name 42 | :param system_name: FDQN of the system generating the metrics 43 | :type system_name: Default: current FDQN 44 | :param suffix: string added to the end of all metrics 45 | :param lowercase_metric_names: Toggle the .lower() of all metric names 46 | :param fqdn_squash: Change host.example.com to host_example_com 47 | :type fqdn_squash: True or False 48 | :param dryrun: Toggle if it will really send metrics or just return them 49 | :type dryrun: True or False 50 | :param timeout_in_seconds: Number of seconds before a connection is timed out. 51 | :param asynchronous: Send messages asynchronouly via gevent (You have to monkey patch sockets for it to work) 52 | :param clean_metric_name: Does GraphiteClient needs to clean metric's name 53 | :type clean_metric_name: True or False 54 | It will then send any metrics that you give it via 55 | the .send() or .send_dict(). 56 | 57 | You can also take advantage of the prefix, group and system_name options 58 | that allow you to setup default locations where your whisper files will 59 | be kept. 60 | eg. 61 | ( where 'linuxserver' is the name of the localhost) 62 | 63 | .. code-block:: python 64 | 65 | >>> init().prefix 66 | systems.linuxserver. 67 | 68 | >>> init(system_name='remote_host').prefix 69 | systems.remote_host. 70 | 71 | >>> init(group='cpu').prefix 72 | systems.linuxserver.cpu. 73 | 74 | >>> init(prefix='apache').prefix 75 | apache. 76 | 77 | """ 78 | 79 | def __init__(self, prefix=None, graphite_server=None, graphite_port=2003, 80 | timeout_in_seconds=2, debug=False, group=None, 81 | system_name=None, suffix=None, lowercase_metric_names=False, 82 | connect_on_create=True, fqdn_squash=False, 83 | dryrun=False, asynchronous=False, autoreconnect=False, 84 | clean_metric_name=True): 85 | """ 86 | setup the connection to the graphite server and work out the 87 | prefix. 88 | 89 | This allows for very simple syntax when sending messages to the 90 | graphite server. 91 | 92 | """ 93 | 94 | # If we are not passed a host, then use the graphite server defined 95 | # in the module. 96 | if not graphite_server: 97 | graphite_server = default_graphite_server 98 | self.addr = (graphite_server, graphite_port) 99 | 100 | # If this is a dry run, then we do not want to configure a connection 101 | # or try and make the connection once we create the object. 102 | self.dryrun = dryrun 103 | if self.dryrun: 104 | self.addr = None 105 | graphite_server = None 106 | connect_on_create = False 107 | 108 | # Only connect to the graphite server and port if we tell you too. 109 | # This is mostly used for testing. 110 | self.timeout_in_seconds = int(timeout_in_seconds) 111 | if connect_on_create: 112 | self.connect() 113 | 114 | self.debug = debug 115 | self.lastmessage = None 116 | 117 | self.asynchronous = False 118 | if asynchronous: 119 | self.asynchronous = self.enable_asynchronous() 120 | self._autoreconnect = autoreconnect 121 | 122 | self.formatter = GraphiteStructuredFormatter(prefix=prefix, group=group, 123 | system_name=system_name, suffix=suffix, 124 | lowercase_metric_names=lowercase_metric_names, fqdn_squash=fqdn_squash, 125 | clean_metric_name=clean_metric_name) 126 | 127 | @property 128 | def prefix(self): 129 | '''Backward compat - access to the properties on the default formatter 130 | deprecated - use the formatter directly for this type of muckery. 131 | ''' 132 | return self.formatter.prefix 133 | 134 | @property 135 | def suffix(self): 136 | '''Backward compat - access to properties on the default formatter 137 | deprecated - use the formatter directly for this type of muckery. 138 | ''' 139 | return self.formatter.suffix 140 | 141 | @property 142 | def lowercase_metric_names(self): 143 | '''Backward compat - access to properties on the default formatter 144 | deprecated - use the formatter directly for this type of muckery. 145 | ''' 146 | return self.formatter.lowercase_metric_names 147 | 148 | def connect(self): 149 | """ 150 | Make a TCP connection to the graphite server on port self.port 151 | """ 152 | self.socket = socket.socket() 153 | self.socket.settimeout(self.timeout_in_seconds) 154 | try: 155 | self.socket.connect(self.addr) 156 | except socket.timeout: 157 | raise GraphiteSendException( 158 | "Took over %d second(s) to connect to %s" % 159 | (self.timeout_in_seconds, self.addr)) 160 | except socket.gaierror: 161 | raise GraphiteSendException( 162 | "No address associated with hostname %s:%s" % self.addr) 163 | except Exception as error: 164 | raise GraphiteSendException( 165 | "unknown exception while connecting to %s - %s" % 166 | (self.addr, error) 167 | ) 168 | 169 | return self.socket 170 | 171 | def reconnect(self): 172 | self.disconnect() 173 | self.connect() 174 | 175 | def autoreconnect(self, sleep=1, attempt=3, exponential=True, jitter=5): 176 | """ 177 | Tries to reconnect with some delay: 178 | 179 | exponential=False: up to `attempt` times with `sleep` seconds between 180 | each try 181 | 182 | exponential=True: up to `attempt` times with exponential growing `sleep` 183 | and random delay in range 1..`jitter` (exponential backoff) 184 | 185 | 186 | :param sleep: time to sleep between two attempts to reconnect 187 | :type sleep: float or int 188 | :param attempt: maximal number of attempts 189 | :type attempt: int 190 | :param exponential: if set - use exponential backoff logic 191 | :type exponential: bool 192 | :param jitter: top value of random delay, sec 193 | :type jitter: int 194 | 195 | """ 196 | 197 | p = 0 198 | 199 | while attempt is None or attempt > 0: 200 | try: 201 | self.reconnect() 202 | return True 203 | except GraphiteSendException: 204 | 205 | if exponential: 206 | p += 1 207 | time.sleep(pow(sleep, p) + random.randint(1, jitter)) 208 | else: 209 | time.sleep(sleep) 210 | 211 | attempt -= 1 212 | 213 | return False 214 | 215 | def clean_metric_name(self, metric_name): 216 | """ 217 | Make sure the metric is free of control chars, spaces, tabs, etc. 218 | """ 219 | return self.formatter.clean_metric_name(metric_name) 220 | 221 | def disconnect(self): 222 | """ 223 | Close the TCP connection with the graphite server. 224 | """ 225 | try: 226 | self.socket.shutdown(1) 227 | 228 | # If its currently a socket, set it to None 229 | except AttributeError: 230 | self.socket = None 231 | except Exception: 232 | self.socket = None 233 | 234 | # Set the self.socket to None, no matter what. 235 | finally: 236 | self.socket = None 237 | 238 | def _dispatch_send(self, message): 239 | """ 240 | Dispatch the different steps of sending 241 | """ 242 | 243 | if self.dryrun: 244 | return message 245 | 246 | if not self.socket: 247 | raise GraphiteSendException( 248 | "Socket was not created before send" 249 | ) 250 | 251 | sending_function = self._send 252 | if self._autoreconnect: 253 | sending_function = self._send_and_reconnect 254 | 255 | try: 256 | if self.asynchronous and gevent: 257 | gevent.spawn(sending_function, message) 258 | else: 259 | sending_function(message) 260 | except Exception as e: 261 | self._handle_send_error(e) 262 | 263 | return "sent {0} long message: {1}".format(len(message), message[:75]) 264 | 265 | def _handle_send_error(self, error): 266 | if isinstance(error, socket.gaierror): 267 | raise GraphiteSendException( 268 | "Failed to send data to %s, with error: %s" % 269 | (self.addr, error)) 270 | 271 | elif isinstance(error, socket.error): 272 | raise GraphiteSendException( 273 | "Socket closed before able to send data to %s, " 274 | "with error: %s" % 275 | (self.addr, error) 276 | ) 277 | 278 | else: 279 | raise GraphiteSendException( 280 | "Unknown error while trying to send data down socket to %s, " 281 | "error: %s" % 282 | (self.addr, error) 283 | ) 284 | 285 | def _send(self, message): 286 | """ 287 | Given a message send it to the graphite server. 288 | """ 289 | 290 | self.socket.sendall(message.encode("ascii")) 291 | 292 | def _send_and_reconnect(self, message): 293 | """Send _message_ to Graphite Server and attempt reconnect on failure. 294 | 295 | If _autoreconnect_ was specified, attempt to reconnect if first send 296 | fails. 297 | 298 | :raises AttributeError: When the socket has not been set. 299 | :raises socket.error: When the socket connection is no longer valid. 300 | """ 301 | try: 302 | self.socket.sendall(message.encode("ascii")) 303 | except (AttributeError, socket.error): 304 | if not self.autoreconnect(): 305 | raise 306 | else: 307 | self.socket.sendall(message.encode("ascii")) 308 | 309 | def _presend(self, message): 310 | """ 311 | Complete any message alteration tasks before sending to the graphite 312 | server. 313 | """ 314 | return message 315 | 316 | def send(self, metric, value, timestamp=None, formatter=None): 317 | """ 318 | Format a single metric/value pair, and send it to the graphite 319 | server. 320 | 321 | :param metric: name of the metric 322 | :type prefix: string 323 | :param value: value of the metric 324 | :type prefix: float or int 325 | :param timestmap: epoch time of the event 326 | :type prefix: float or int 327 | :param formatter: option non-default formatter 328 | :type prefix: callable 329 | 330 | .. code-block:: python 331 | 332 | >>> g = init() 333 | >>> g.send("metric", 54) 334 | 335 | .. code-block:: python 336 | 337 | >>> g = init() 338 | >>> g.send(metric="metricname", value=73) 339 | 340 | """ 341 | if formatter is None: 342 | formatter = self.formatter 343 | message = formatter(metric, value, timestamp) 344 | message = self. _presend(message) 345 | return self._dispatch_send(message) 346 | 347 | def send_dict(self, data, timestamp=None, formatter=None): 348 | """ 349 | Format a dict of metric/values pairs, and send them all to the 350 | graphite server. 351 | 352 | :param data: key,value pair of metric name and metric value 353 | :type prefix: dict 354 | :param timestmap: epoch time of the event 355 | :type prefix: float or int 356 | :param formatter: option non-default formatter 357 | :type prefix: callable 358 | 359 | .. code-block:: python 360 | 361 | >>> g = init() 362 | >>> g.send_dict({'metric1': 54, 'metric2': 43, 'metricN': 999}) 363 | 364 | """ 365 | if formatter is None: 366 | formatter = self.formatter 367 | 368 | metric_list = [] 369 | 370 | for metric, value in data.items(): 371 | tmp_message = formatter(metric, value, timestamp) 372 | metric_list.append(tmp_message) 373 | 374 | message = "".join(metric_list) 375 | return self._dispatch_send(message) 376 | 377 | def send_list(self, data, timestamp=None, formatter=None): 378 | """ 379 | 380 | Format a list of set's of (metric, value) pairs, and send them all 381 | to the graphite server. 382 | 383 | :param data: list of key,value pairs of metric name and metric value 384 | :type prefix: list 385 | :param timestmap: epoch time of the event 386 | :type prefix: float or int 387 | :param formatter: option non-default formatter 388 | :type prefix: callable 389 | 390 | .. code-block:: python 391 | 392 | >>> g = init() 393 | >>> g.send_list([('metric1', 54),('metric2', 43, 1384418995)]) 394 | 395 | """ 396 | if formatter is None: 397 | formatter = self.formatter 398 | 399 | if timestamp is None: 400 | timestamp = int(time.time()) 401 | else: 402 | timestamp = int(timestamp) 403 | 404 | metric_list = [] 405 | 406 | for metric_info in data: 407 | 408 | # Support [ (metric, value, timestamp), ... ] as well as 409 | # [ (metric, value), ... ]. 410 | # If the metric_info provides a timestamp then use the timestamp. 411 | # If the metric_info fails to provide a timestamp, use the one 412 | # provided to send_list() or generated on the fly by time.time() 413 | if len(metric_info) == 3: 414 | (metric, value, metric_timestamp) = metric_info 415 | else: 416 | (metric, value) = metric_info 417 | metric_timestamp = timestamp 418 | 419 | tmp_message = formatter(metric, value, metric_timestamp) 420 | metric_list.append(tmp_message) 421 | 422 | message = "".join(metric_list) 423 | return self._dispatch_send(message) 424 | 425 | def enable_asynchronous(self): 426 | """Check if socket have been monkey patched by gevent""" 427 | 428 | def is_monkey_patched(): 429 | try: 430 | from gevent import monkey, socket 431 | except ImportError: 432 | return False 433 | if hasattr(monkey, "saved"): 434 | return "socket" in monkey.saved 435 | return gevent.socket.socket == socket.socket 436 | 437 | if not is_monkey_patched(): 438 | raise Exception("To activate asynchonoucity, please monkey patch" 439 | " the socket module with gevent") 440 | return True 441 | 442 | 443 | class GraphitePickleClient(GraphiteClient): 444 | 445 | def __init__(self, *args, **kwargs): 446 | # If the user has not given a graphite_port, then use the default pick 447 | # port. 448 | if 'graphite_port' not in kwargs: 449 | kwargs['graphite_port'] = default_graphite_pickle_port 450 | 451 | # TODO: Fix this hack and use super. 452 | # self = GraphiteClient(*args, **kwargs) # noqa 453 | super(self.__class__, self).__init__(*args, **kwargs) 454 | 455 | def str2listtuple(self, string_message): 456 | "Covert a string that is ready to be sent to graphite into a tuple" 457 | 458 | if type(string_message).__name__ not in ('str', 'unicode'): 459 | raise TypeError("Must provide a string or unicode") 460 | 461 | if not string_message.endswith('\n'): 462 | string_message += "\n" 463 | 464 | tpl_list = [] 465 | for line in string_message.split('\n'): 466 | line = line.strip() 467 | if not line: 468 | continue 469 | path, metric, timestamp = (None, None, None) 470 | try: 471 | (path, metric, timestamp) = line.split() 472 | except ValueError: 473 | raise ValueError( 474 | "message must contain - metric_name, value and timestamp '%s'" 475 | % line) 476 | try: 477 | timestamp = float(timestamp) 478 | except ValueError: 479 | raise ValueError("Timestamp must be float or int") 480 | 481 | tpl_list.append((path, (timestamp, metric))) 482 | 483 | if len(tpl_list) == 0: 484 | raise GraphiteSendException("No messages to send") 485 | 486 | payload = pickle.dumps(tpl_list) 487 | header = struct.pack("!L", len(payload)) 488 | message = header + payload 489 | 490 | return message 491 | 492 | def _send(self, message): 493 | """ Given a message send it to the graphite server. """ 494 | 495 | # An option to lowercase the entire message 496 | if self.lowercase_metric_names: 497 | message = message.lower() 498 | 499 | # convert the message into a pickled payload. 500 | message = self.str2listtuple(message) 501 | 502 | try: 503 | self.socket.sendall(message) 504 | # Capture missing socket. 505 | except socket.gaierror as error: 506 | raise GraphiteSendException( 507 | "Failed to send data to %s, with error: %s" % 508 | (self.addr, error)) # noqa 509 | 510 | # Capture socket closure before send. 511 | except socket.error as error: 512 | raise GraphiteSendException( 513 | "Socket closed before able to send data to %s, " 514 | "with error: %s" % 515 | (self.addr, error)) # noqa 516 | 517 | except Exception as error: 518 | raise GraphiteSendException( 519 | "Unknown error while trying to send data down socket to %s, " 520 | "error: %s" % 521 | (self.addr, error)) # noqa 522 | 523 | return "sent %d long pickled message" % len(message) 524 | 525 | 526 | def init(init_type='plaintext_tcp', *args, **kwargs): 527 | """ 528 | Create the module instance of the GraphiteClient. 529 | """ 530 | global _module_instance 531 | reset() 532 | 533 | validate_init_types = ['plaintext_tcp', 'plaintext', 'pickle_tcp', 534 | 'pickle', 'plain'] 535 | 536 | if init_type not in validate_init_types: 537 | raise GraphiteSendException( 538 | "Invalid init_type '%s', must be one of: %s" % 539 | (init_type, ", ".join(validate_init_types))) 540 | 541 | # Use TCP to send data to the plain text receiver on the graphite server. 542 | if init_type in ['plaintext_tcp', 'plaintext', 'plain']: 543 | _module_instance = GraphiteClient(*args, **kwargs) 544 | 545 | # Use TCP to send pickled data to the pickle receiver on the graphite 546 | # server. 547 | if init_type in ['pickle_tcp', 'pickle']: 548 | _module_instance = GraphitePickleClient(*args, **kwargs) 549 | 550 | return _module_instance 551 | 552 | 553 | def send(*args, **kwargs): 554 | """ Make sure that we have an instance of the GraphiteClient. 555 | Then send the metrics to the graphite server. 556 | User consumable method. 557 | """ 558 | if not _module_instance: 559 | raise GraphiteSendException( 560 | "Must call graphitesend.init() before sending") 561 | 562 | _module_instance.send(*args, **kwargs) 563 | return _module_instance 564 | 565 | 566 | def send_dict(*args, **kwargs): 567 | """ Make sure that we have an instance of the GraphiteClient. 568 | Then send the metrics to the graphite server. 569 | User consumable method. 570 | """ 571 | if not _module_instance: 572 | raise GraphiteSendException( 573 | "Must call graphitesend.init() before sending") 574 | _module_instance.send_dict(*args, **kwargs) 575 | return _module_instance 576 | 577 | 578 | def send_list(*args, **kwargs): 579 | """ Make sure that we have an instance of the GraphiteClient. 580 | Then send the metrics to the graphite server. 581 | User consumable method. 582 | """ 583 | if not _module_instance: 584 | raise GraphiteSendException( 585 | "Must call graphitesend.init() before sending") 586 | _module_instance.send_list(*args, **kwargs) 587 | return _module_instance 588 | 589 | 590 | def reset(): 591 | """ disconnect from the graphite server and destroy the module instance. 592 | """ 593 | global _module_instance 594 | if not _module_instance: 595 | return False 596 | _module_instance.disconnect() 597 | _module_instance = None 598 | 599 | 600 | def cli(): 601 | """ Allow the module to be called from the cli. """ 602 | import argparse 603 | 604 | parser = argparse.ArgumentParser(description='Send data to graphite') 605 | 606 | # Core of the application is to accept a metric and a value. 607 | parser.add_argument('metric', metavar='metric', type=str, 608 | help='name.of.metric') 609 | parser.add_argument('value', metavar='value', type=int, 610 | help='value of metric as int') 611 | 612 | args = parser.parse_args() 613 | metric = args.metric 614 | value = args.value 615 | 616 | graphitesend_instance = init() 617 | graphitesend_instance.send(metric, value) 618 | 619 | if __name__ == '__main__': # pragma: no cover 620 | cli() 621 | -------------------------------------------------------------------------------- /requirements-asynchronous.txt: -------------------------------------------------------------------------------- 1 | -r requirements.txt 2 | gevent 3 | -------------------------------------------------------------------------------- /requirements-test.txt: -------------------------------------------------------------------------------- 1 | -r requirements.txt 2 | coverage 3 | nose 4 | pep8 5 | tox 6 | coveralls 7 | unittest2 8 | 9 | # py26... 10 | flake8==2.6.2 11 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | argparse 2 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from distutils.core import setup 3 | 4 | setup( 5 | name='graphitesend', 6 | version='0.10.0', 7 | description='A simple interface for sending metrics to Graphite', 8 | author='Danny Lawrence', 9 | author_email='dannyla@linux.com', 10 | url='https://github.com/daniellawrence/graphitesend', 11 | packages=['graphitesend'], 12 | long_description="https://github.com/daniellawrence/graphitesend", 13 | entry_points={ 14 | 'console_scripts': [ 15 | 'graphitesend = graphitesend.graphitesend:cli', 16 | ], 17 | }, 18 | extras_require={ 19 | 'asynchronous': ['gevent>=1.0.0'], 20 | } 21 | ) 22 | -------------------------------------------------------------------------------- /tests/test_all.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from graphitesend import graphitesend 4 | import unittest2 as unittest 5 | import socket 6 | import os 7 | 8 | 9 | class TestAll(unittest.TestCase): 10 | """ Basic tests ( better than nothing ) """ 11 | 12 | def setUp(self): 13 | """ reset graphitesend """ 14 | # Drop any connections or modules that have been setup from other tests 15 | graphitesend.reset() 16 | # Monkeypatch the graphitesend so that it points at a graphite service 17 | # running on one of my (dannyla@linux.com) systems. 18 | # graphitesend.default_graphite_server = 'graphite.dansysadm.com' 19 | graphitesend.default_graphite_server = 'localhost' 20 | self.hostname = os.uname()[1] 21 | self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 22 | self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 23 | self.server.bind(('localhost', 2003)) 24 | self.server.listen(5) 25 | 26 | def tearDown(self): 27 | """ reset graphitesend """ 28 | # Drop any connections or modules that have been setup from other tests 29 | graphitesend.reset() 30 | try: 31 | self.server.shutdown(socket.SHUT_RD) 32 | self.server.close() 33 | except Exception: 34 | pass 35 | self.server = None 36 | 37 | def test_connect_exception_on_badhost(self): 38 | bad_graphite_server = 'missinggraphiteserver.example.com' 39 | graphitesend.default_graphite_server = bad_graphite_server 40 | with self.assertRaises(graphitesend.GraphiteSendException): 41 | graphitesend.init() 42 | 43 | def test_set_lowercase_metric_names(self): 44 | g = graphitesend.init(lowercase_metric_names=True) 45 | self.assertEqual(g.lowercase_metric_names, True) 46 | 47 | def test_create_graphitesend_instance(self): 48 | g = graphitesend.init() 49 | expected_type = type(graphitesend.GraphiteClient()) 50 | g_type = type(g) 51 | self.assertEqual(g_type, expected_type) 52 | 53 | def test_monkey_patch_of_graphitehost(self): 54 | g = graphitesend.init() 55 | custom_prefix = g.addr[0] 56 | self.assertEqual(custom_prefix, 'localhost') 57 | 58 | def test_fqdn_squash(self): 59 | g = graphitesend.init(fqdn_squash=True) 60 | custom_prefix = g.prefix 61 | expected_results = 'systems.%s.' % self.hostname.replace('.', '_') 62 | self.assertEqual(custom_prefix, expected_results) 63 | 64 | def test_noprefix(self): 65 | g = graphitesend.init() 66 | custom_prefix = g.prefix 67 | self.assertEqual(custom_prefix, 'systems.%s.' % self.hostname) 68 | 69 | def test_system_name(self): 70 | g = graphitesend.init(system_name='remote_host') 71 | custom_prefix = g.prefix 72 | expected_prefix = 'systems.remote_host.' 73 | self.assertEqual(custom_prefix, expected_prefix) 74 | 75 | def test_empty_system_name(self): 76 | g = graphitesend.init(system_name='') 77 | custom_prefix = g.prefix 78 | expected_prefix = 'systems.' 79 | self.assertEqual(custom_prefix, expected_prefix) 80 | 81 | def test_no_system_name(self): 82 | g = graphitesend.init(group='foo') 83 | custom_prefix = g.prefix 84 | expected_prefix = 'systems.%s.foo.' % self.hostname 85 | self.assertEqual(custom_prefix, expected_prefix) 86 | 87 | def test_prefix(self): 88 | g = graphitesend.init(prefix='custom_prefix') 89 | custom_prefix = g.prefix 90 | self.assertEqual(custom_prefix, 'custom_prefix.%s.' % self.hostname) 91 | 92 | def test_prefix_double_dot(self): 93 | g = graphitesend.init(prefix='custom_prefix.') 94 | custom_prefix = g.prefix 95 | self.assertEqual(custom_prefix, 'custom_prefix.%s.' % self.hostname) 96 | 97 | def test_prefix_remove_spaces(self): 98 | g = graphitesend.init(prefix='custom prefix') 99 | custom_prefix = g.prefix 100 | self.assertEqual(custom_prefix, 'custom_prefix.%s.' % self.hostname) 101 | 102 | def test_set_prefix_group(self): 103 | g = graphitesend.init(prefix='prefix', group='group') 104 | custom_prefix = g.prefix 105 | expected_prefix = 'prefix.%s.group.' % self.hostname 106 | self.assertEqual(custom_prefix, expected_prefix) 107 | 108 | def test_set_prefix_group_system(self): 109 | g = graphitesend.init(prefix='prefix', system_name='system', 110 | group='group') 111 | custom_prefix = g.prefix 112 | expected_prefix = 'prefix.system.group.' 113 | self.assertEqual(custom_prefix, expected_prefix) 114 | 115 | def test_set_suffix(self): 116 | g = graphitesend.init(suffix='custom_suffix') 117 | custom_suffix = g.suffix 118 | self.assertEqual(custom_suffix, 'custom_suffix') 119 | 120 | def test_set_group_prefix(self): 121 | g = graphitesend.init(group='custom_group') 122 | expected_prefix = "systems.%s.custom_group." % self.hostname 123 | custom_prefix = g.prefix 124 | self.assertEqual(custom_prefix, expected_prefix) 125 | 126 | def test_default_prefix(self): 127 | g = graphitesend.init() 128 | expected_prefix = "systems.%s." % self.hostname 129 | custom_prefix = g.prefix 130 | self.assertEqual(custom_prefix, expected_prefix) 131 | 132 | def test_leave_suffix(self): 133 | g = graphitesend.init() 134 | default_suffix = g.suffix 135 | self.assertEqual(default_suffix, '') 136 | 137 | def test_clean_metric(self): 138 | g = graphitesend.init() 139 | # 140 | metric_name = g.clean_metric_name('test(name)') 141 | self.assertEqual(metric_name, 'test_name') 142 | # 143 | metric_name = g.clean_metric_name('test name') 144 | self.assertEqual(metric_name, 'test_name') 145 | # 146 | metric_name = g.clean_metric_name('test name') 147 | self.assertEqual(metric_name, 'test__name') 148 | 149 | def test_clean_metric_false(self): 150 | g = graphitesend.init(clean_metric_name=False) 151 | 152 | metric_name = g.clean_metric_name('test(name)') 153 | self.assertEqual(metric_name, 'test(name)') 154 | 155 | metric_name = g.clean_metric_name('test name') 156 | self.assertEqual(metric_name, 'test name') 157 | 158 | metric_name = g.clean_metric_name('test__name') 159 | self.assertEqual(metric_name, 'test__name') 160 | 161 | def test_reset(self): 162 | graphitesend.init() 163 | graphitesend.reset() 164 | graphite_instance = graphitesend._module_instance 165 | self.assertEqual(graphite_instance, None) 166 | 167 | def test_force_failure_on_send(self): 168 | graphite_instance = graphitesend.init() 169 | graphite_instance.disconnect() 170 | with self.assertRaises(graphitesend.GraphiteSendException): 171 | graphite_instance.send('metric', 0) 172 | 173 | def test_force_unknown_failure_on_send(self): 174 | graphite_instance = graphitesend.init() 175 | graphite_instance.socket = None 176 | with self.assertRaises(graphitesend.GraphiteSendException): 177 | graphite_instance.send('metric', 0) 178 | 179 | def test_send_list_metric_value(self): 180 | graphite_instance = graphitesend.init(prefix='test', system_name='local') 181 | response = graphite_instance.send_list([('metric', 1)]) 182 | self.assertEqual('long message: test.local.metric 1' in response, True) 183 | self.assertEqual('1.00000' in response, True) 184 | 185 | def test_send_list_metric_value_timestamp_2(self): 186 | graphite_instance = graphitesend.init(prefix='test', system_name='') 187 | # Make sure it can handle custom timestamp 188 | response = graphite_instance.send_list( 189 | [('metric', 1, 1), ('metric', 1, 2)]) 190 | # self.assertEqual('sent 46 long message:' in response, True) 191 | self.assertEqual('test.metric 1.000000 1' in response, True) 192 | self.assertEqual('test.metric 1.000000 2' in response, True) 193 | 194 | def test_send_list_metric_value_timestamp_3(self): 195 | graphite_instance = graphitesend.init(prefix='test', system_name='') 196 | # Make sure it can handle custom timestamp, fill in the missing with 197 | # the current time. 198 | response = graphite_instance.send_list( 199 | [ 200 | ('metric', 1, 1), 201 | ('metric', 2), 202 | ] 203 | ) 204 | 205 | # self.assertEqual('sent 46 long message:' in response, True) 206 | self.assertEqual('test.metric 1.000000 1' in response, True) 207 | self.assertEqual('test.metric 2.000000 2' not in response, True) 208 | 209 | def test_send_list_metric_value_timestamp_default(self): 210 | graphite_instance = graphitesend.init(prefix='test', system_name='bar') 211 | # Make sure it can handle custom timestamp, fill in the missing with 212 | # the current time. 213 | response = graphite_instance.send_list( 214 | [ 215 | ('metric', 1, 1), 216 | ('metric', 2), 217 | ], 218 | timestamp='4' 219 | ) 220 | # self.assertEqual('sent 69 long message:' in response, True) 221 | self.assertEqual('test.bar.metric 1.000000 1' in response, True) 222 | self.assertEqual('test.bar.metric 2.000000 4' in response, True) 223 | 224 | def test_send_list_metric_value_timestamp_default_2(self): 225 | graphite_instance = graphitesend.init(prefix='test', system_name='foo') 226 | # Make sure it can handle custom timestamp, fill in the missing with 227 | # the current time. 228 | (c, addr) = self.server.accept() 229 | response = graphite_instance.send_list( 230 | [ 231 | ('metric', 1), 232 | ('metric', 2, 2), 233 | ], 234 | timestamp='4' 235 | ) 236 | response = str(response) 237 | # self.assertEqual('sent 69 long message:' in response, True) 238 | self.assertEqual('test.foo.metric 1.000000 4' in response, True) 239 | self.assertEqual('test.foo.metric 2.000000 2' in response, True) 240 | sent_on_socket = str(c.recv(69)) 241 | self.assertEqual('test.foo.metric 1.000000 4' in sent_on_socket, True) 242 | self.assertEqual('test.foo.metric 2.000000 2' in sent_on_socket, True) 243 | # self.server.shutdown(socket.SHUT_RD) 244 | # self.server.close() 245 | 246 | def test_send_metric_as_integer(self): 247 | # Make sure it can handle integer as metric name 248 | graphite_instance = graphitesend.init(prefix='') 249 | response = graphite_instance.send(42, "1", "1") 250 | self.assertEqual('42' in response, True) 251 | 252 | 253 | if __name__ == '__main__': 254 | unittest.main() 255 | -------------------------------------------------------------------------------- /tests/test_async.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from gevent import monkey 4 | from graphitesend import graphitesend 5 | import unittest2 as unittest 6 | import socket 7 | 8 | monkey.patch_socket() 9 | 10 | 11 | class TestAsync(unittest.TestCase): 12 | """ Basic tests ( better than nothing ) """ 13 | 14 | def setUp(self): 15 | """ reset graphitesend """ 16 | self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 17 | self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 18 | self.server.bind(('localhost', 2003)) 19 | self.server.listen(5) 20 | 21 | def tearDown(self): 22 | """ reset graphitesend """ 23 | # Drop any connections or modules that have been setup from other tests 24 | graphitesend.reset() 25 | try: 26 | self.server.shutdown(socket.SHUT_RD) 27 | self.server.close() 28 | except Exception: 29 | pass 30 | self.server = None 31 | 32 | def test_set_async_default(self): 33 | g = graphitesend.init(dryrun=True) 34 | self.assertEqual(g.asynchronous, False) 35 | 36 | def test_set_async_true(self): 37 | g = graphitesend.init(dryrun=True, asynchronous=True) 38 | self.assertEqual(g.asynchronous, True) 39 | 40 | def test_set_async_false(self): 41 | g = graphitesend.init(dryrun=True, asynchronous=False) 42 | self.assertEqual(g.asynchronous, False) 43 | 44 | def test_send_reconnect_send_again(self): 45 | g = graphitesend.init(prefix='', system_name='', asynchronous=True) 46 | g.send('test_send', 50) 47 | (c, addr) = self.server.accept() 48 | sent_on_socket = str(c.recv(69)) 49 | self.assertIn('test_send 50.000000', sent_on_socket) 50 | -------------------------------------------------------------------------------- /tests/test_autoreconnect.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from graphitesend import graphitesend 4 | import unittest2 as unittest 5 | import socket 6 | 7 | 8 | class TestAutoreconnect(unittest.TestCase): 9 | 10 | def setUp(self): 11 | """ reset graphitesend """ 12 | self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 13 | self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 14 | self.server.bind(('localhost', 2003)) 15 | self.server.listen(5) 16 | 17 | def tearDown(self): 18 | """ reset graphitesend """ 19 | # Drop any connections or modules that have been setup from other tests 20 | graphitesend.reset() 21 | try: 22 | self.server.shutdown(socket.SHUT_RD) 23 | self.server.close() 24 | except Exception: 25 | pass 26 | self.server = None 27 | 28 | def test_set_autoreconnect_default(self): 29 | g = graphitesend.init(dryrun=True) 30 | self.assertEqual(g._autoreconnect, False) 31 | 32 | def test_set_autoreconnect_true(self): 33 | g = graphitesend.init(dryrun=True, autoreconnect=True) 34 | self.assertEqual(g._autoreconnect, True) 35 | 36 | def test_set_autoreconnect_false(self): 37 | g = graphitesend.init(dryrun=True, autoreconnect=False) 38 | self.assertEqual(g._autoreconnect, False) 39 | 40 | def test_autoreconnect(self): 41 | g = graphitesend.GraphiteClient(autoreconnect=True) 42 | g.send("metric", 42) 43 | self.tearDown() 44 | with self.assertRaises(graphitesend.GraphiteSendException): 45 | g.send("metric", 2) 46 | self.setUp() 47 | g.send("metric", 3) 48 | -------------------------------------------------------------------------------- /tests/test_cli.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import unittest2 as unittest 3 | from graphitesend import graphitesend 4 | import os 5 | import socket 6 | 7 | 8 | class TestCli(unittest.TestCase): 9 | """ Tests to make sure that a dryrun is just that. """ 10 | 11 | def setUp(self): 12 | """ reset graphitesend """ 13 | # Drop any connections or modules that have been setup from other tests 14 | graphitesend.reset() 15 | graphitesend.default_graphite_server = 'localhost' 16 | self.hostname = os.uname()[1] 17 | 18 | self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 19 | self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 20 | self.server.bind(('localhost', 2003)) 21 | self.server.listen(5) 22 | 23 | self.pserver = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 24 | self.pserver.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 25 | self.pserver.bind(('localhost', 2004)) 26 | self.pserver.listen(5) 27 | 28 | def tearDown(self): 29 | """ reset graphitesend """ 30 | # Drop any connections or modules that have been setup from other tests 31 | graphitesend.reset() 32 | try: 33 | self.server.shutdown(socket.SHUT_RD) 34 | self.server.close() 35 | except Exception: 36 | pass 37 | self.server = None 38 | 39 | try: 40 | self.pserver.shutdown(socket.SHUT_RD) 41 | self.pserver.close() 42 | except Exception: 43 | pass 44 | self.pserver = None 45 | 46 | def test_cli(self): 47 | with self.assertRaises(SystemExit): 48 | graphitesend.cli() 49 | 50 | sys.argv = ['graphitesend_cli_test', 'test_cli_metric', '50'] 51 | graphitesend.cli() 52 | (c, addr) = self.server.accept() 53 | sent_on_socket = str(c.recv(1024)) 54 | self.assertIn('test_cli_metric 50.000000', sent_on_socket) 55 | 56 | def test_send_list(self): 57 | with self.assertRaises(graphitesend.GraphiteSendException): 58 | graphitesend.send_list([('test_metric', 50), ]) 59 | 60 | graphitesend.init(system_name='') 61 | graphitesend.send_list([('test_send_list', 50), ]) 62 | (c, addr) = self.server.accept() 63 | sent_on_socket = str(c.recv(69)) 64 | print(sent_on_socket) 65 | self.assertIn('test_send_list 50.000000', sent_on_socket) 66 | 67 | def test_send_dict(self): 68 | with self.assertRaises(graphitesend.GraphiteSendException): 69 | graphitesend.send_dict({'test_metric': 50}) 70 | 71 | graphitesend.init(system_name='') 72 | graphitesend.send_dict({'test_send_dict': 50}) 73 | (c, addr) = self.server.accept() 74 | sent_on_socket = str(c.recv(69)) 75 | print(sent_on_socket) 76 | self.assertIn('test_send_dict 50.000000', sent_on_socket) 77 | 78 | def test_send_dict_with_timestamp(self): 79 | graphitesend.init(system_name='') 80 | graphitesend.send_dict({'test_send_dict': 50}, 1) 81 | (c, addr) = self.server.accept() 82 | sent_on_socket = str(c.recv(69)) 83 | print(sent_on_socket) 84 | self.assertIn('test_send_dict 50.000000 1', sent_on_socket) 85 | 86 | def test_send(self): 87 | with self.assertRaises(graphitesend.GraphiteSendException): 88 | graphitesend.send('test_metric', 50) 89 | 90 | graphitesend.init(system_name='') 91 | graphitesend.send('test_send', 50) 92 | (c, addr) = self.server.accept() 93 | sent_on_socket = str(c.recv(69)) 94 | self.assertIn('test_send 50.000000', sent_on_socket) 95 | 96 | def test_init_as_pickel(self): 97 | g = graphitesend.init() 98 | self.assertEqual(type(g), type(graphitesend.GraphiteClient())) 99 | 100 | for init_type in ['plaintext_tcp', 'plaintext', 'plain']: 101 | g = graphitesend.init(init_type=init_type, dryrun=True) 102 | self.assertEqual(type(g), type(graphitesend.GraphiteClient())) 103 | 104 | for init_type in ['pickle', 'pickle_tcp']: 105 | g = graphitesend.init(init_type=init_type, dryrun=True) 106 | self.assertEqual(type(g), 107 | type(graphitesend.GraphitePickleClient())) 108 | 109 | def test_init_bad_type(self): 110 | with self.assertRaises(graphitesend.GraphiteSendException): 111 | graphitesend.init(init_type="bad_type", dryrun=True) 112 | 113 | def test_socket_timeout(self): 114 | with self.assertRaises(graphitesend.GraphiteSendException): 115 | graphitesend.init(timeout_in_seconds=.0000000000001) 116 | 117 | def test_send_prefix_empty(self): 118 | graphitesend.init(prefix='', system_name='') 119 | graphitesend.send('test_send', 50) 120 | (c, addr) = self.server.accept() 121 | sent_on_socket = str(c.recv(69)) 122 | self.assertIn('test_send 50.000000', sent_on_socket) 123 | 124 | def test_send_reconnect_send_again(self): 125 | g = graphitesend.init(prefix='', system_name='') 126 | g.send('test_send', 50) 127 | (c, addr) = self.server.accept() 128 | sent_on_socket = str(c.recv(69)) 129 | self.assertIn('test_send 50.000000', sent_on_socket) 130 | g.reconnect() 131 | g.send('test_send', 50) 132 | (c, addr) = self.server.accept() 133 | sent_on_socket = str(c.recv(69)) 134 | self.assertIn('test_send 50.000000', sent_on_socket) 135 | 136 | def test_dryrun(self): 137 | g = graphitesend.init(dryrun=True) 138 | dryrun_messsage_send = 'testing dryrun' 139 | dryrun_messsage_recv = g._dispatch_send(dryrun_messsage_send) 140 | self.assertEqual(dryrun_messsage_recv, dryrun_messsage_send) 141 | 142 | def test_send_gaierror(self): 143 | g = graphitesend.init() 144 | g.socket = True 145 | with self.assertRaises(graphitesend.GraphiteSendException): 146 | g._dispatch_send('test') 147 | 148 | def test_str2listtuple_bad(self): 149 | g = graphitesend.init(init_type='pickle') 150 | with self.assertRaises(TypeError): 151 | g.str2listtuple(54) 152 | with self.assertRaises(TypeError): 153 | g.str2listtuple([]) 154 | with self.assertRaises(TypeError): 155 | g.str2listtuple({}) 156 | with self.assertRaises(ValueError): 157 | g.str2listtuple("metric") 158 | 159 | with self.assertRaises(ValueError): 160 | g.str2listtuple("metric value timestamp extra") 161 | 162 | def test_str2listtuple_good(self): 163 | if sys.version_info[0] == 3: 164 | return 165 | 166 | g = graphitesend.init(init_type='pickle') 167 | pickle_response = g.str2listtuple("path metric 1") 168 | self.assertEqual( 169 | pickle_response, 170 | "\x00\x00\x00.(lp0\n(S'path'\np1\n" + 171 | "(F1.0\nS'metric'\np2\ntp3\ntp4\na." 172 | ) 173 | 174 | def test_send_list_str_to_int(self): 175 | graphitesend.init(system_name='') 176 | graphitesend.send_list([('test_send_list', '50'), ]) 177 | (c, addr) = self.server.accept() 178 | sent_on_socket = str(c.recv(69)) 179 | self.assertIn('test_send_list 50.000000', sent_on_socket) 180 | 181 | def test_send_dict_str_to_int(self): 182 | graphitesend.init(system_name='') 183 | graphitesend.send_dict({'test_send_dict': '50'}) 184 | (c, addr) = self.server.accept() 185 | sent_on_socket = str(c.recv(69)) 186 | self.assertIn('test_send_dict 50.000000', sent_on_socket) 187 | 188 | def test_pickle_send(self): 189 | if sys.version_info[0] == 3: 190 | return 191 | 192 | g = graphitesend.init(init_type='pickle', system_name='', prefix='') 193 | (c, addr) = self.pserver.accept() 194 | g.send('test_pickle', 50, 0) 195 | sent_on_socket = str(c.recv(69)) 196 | self.assertEqual( 197 | sent_on_socket, 198 | "\x00\x00\x008(lp0\n(S'test_pickle'\np1\n(F0.0\nS'50.000000'\np2\ntp3\ntp4\na." 199 | ) 200 | -------------------------------------------------------------------------------- /tests/test_dryrun.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import unittest 4 | from graphitesend import graphitesend 5 | import os 6 | import socket 7 | 8 | 9 | class TestDryRun(unittest.TestCase): 10 | """ Tests to make sure that a dryrun is just that. """ 11 | 12 | def setUp(self): 13 | """ reset graphitesend """ 14 | # Drop any connections or modules that have been setup from other tests 15 | graphitesend.reset() 16 | graphitesend.default_graphite_server = 'localhost' 17 | self.hostname = os.uname()[1] 18 | 19 | self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 20 | self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 21 | self.server.bind(('localhost', 2003)) 22 | self.server.listen(5) 23 | 24 | def tearDown(self): 25 | """ reset graphitesend """ 26 | # Drop any connections or modules that have been setup from other tests 27 | graphitesend.reset() 28 | try: 29 | self.server.shutdown(socket.SHUT_RD) 30 | self.server.close() 31 | except Exception: 32 | pass 33 | self.server = None 34 | 35 | def test_EmptyAddr(self): 36 | g = graphitesend.init(prefix='', dryrun=True) 37 | self.assertEqual(g.addr, None) 38 | 39 | def test_CreateGraphiteClient(self): 40 | g = graphitesend.init(prefix='', dryrun=True) 41 | self.assertEqual(type(g).__name__, 'GraphiteClient') 42 | dryrun_message = g.send('metric', 1, 1) 43 | self.assertEqual(dryrun_message, 44 | "%s.metric 1.000000 1\n" % self.hostname) 45 | 46 | def test_DryrunConnectFailure(self): 47 | g = graphitesend.init(prefix='', dryrun=True) 48 | self.assertEqual(type(g).__name__, 'GraphiteClient') 49 | with self.assertRaises(graphitesend.GraphiteSendException): 50 | g.connect() 51 | 52 | def test_BadGraphtieServer(self): 53 | graphitesend.default_graphite_server = "BADGRAPHITESERVER" 54 | g = graphitesend.init(prefix='', dryrun=True) 55 | self.assertEqual(type(g).__name__, 'GraphiteClient') 56 | dryrun_message = g.send('metric', 1, 1) 57 | self.assertEqual(dryrun_message, 58 | "%s.metric 1.000000 1\n" % self.hostname) 59 | -------------------------------------------------------------------------------- /tests/test_housekeeping.py: -------------------------------------------------------------------------------- 1 | import unittest2 as unittest 2 | import os 3 | import datetime 4 | 5 | 6 | class HouseKeeping(unittest.TestCase): 7 | 8 | def test_license_year(self): 9 | self.assertTrue(os.path.exists('LICENSE.txt')) 10 | now = datetime.datetime.now() 11 | current_year = datetime.datetime.strftime(now, '%Y') 12 | license_text = open('LICENSE.txt').read() 13 | expected_text = 'Copyright %s Danny Lawrence ' \ 14 | % current_year 15 | self.assertIn(expected_text, license_text) 16 | 17 | def test_pip_install(self): 18 | x = os.popen("pip uninstall graphitesend -y") 19 | print(x.read()) 20 | y = os.popen("pip install -e .") 21 | print(y.read()) 22 | pip_freeze_stdout = os.popen("pip freeze").read() 23 | self.assertIn("graphitesend", pip_freeze_stdout) 24 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist=py27,py35 3 | 4 | [flake8] 5 | ignore=E128,E501 6 | 7 | [testenv:flake8] 8 | deps = flake8 9 | commands = flake8 --exclude=.tox,.virtualenv --ignore=E501 10 | 11 | [testenv:nosetests] 12 | deps = 13 | gevent 14 | -rrequirements-test.txt 15 | commands = nosetests --with-coverage --cover-package=graphitesend 16 | 17 | [testenv:nosetests-dbg] 18 | deps = 19 | gevent 20 | -rrequirements-test.txt 21 | commands = nosetests --with-coverage --cover-package=graphitesend -s 22 | 23 | [testenv:90%coverage] 24 | deps = 25 | flake8 26 | gevent 27 | -rrequirements-test.txt 28 | commands = 29 | flake8 --exclude=.tox,.virtualenv --ignore=E501 30 | nosetests --with-coverage --cover-package=graphitesend 31 | --------------------------------------------------------------------------------