{"id":1892,"date":"2015-11-16T00:01:21","date_gmt":"2015-11-15T15:01:21","guid":{"rendered":"http:\/\/www.magtranetwork.com\/?p=1892"},"modified":"2016-12-06T09:38:58","modified_gmt":"2016-12-06T00:38:58","slug":"aws_lambda_python_speedtest_cli","status":"publish","type":"post","link":"https:\/\/www.magtranetwork.com\/aws\/aws_lambda_python_speedtest_cli.html","title":{"rendered":"AWS Lambda Python\u3067Lambda\u95a2\u6570\u5b9f\u884c\u30b5\u30fc\u30d0\u306e\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u901f\u5ea6\u30c6\u30b9\u30c8\u3092speedtest-cli\u3092\u7528\u3044\u3066\u884c\u3046"},"content":{"rendered":"
AWS Lambda\u3092\u4f7f\u7528\u3059\u308b\u4e0a\u3067Lambda\u95a2\u6570\u3092\u5b9f\u884c\u3059\u308b\u30b5\u30fc\u30d0\u304c\u3069\u306e\u7a0b\u5ea6\u306e\u6027\u80fd\u3092\u6301\u3063\u3066\u3044\u308b\u304b\u306f\u6c17\u306b\u306a\u308b\u3068\u3053\u308d\u3067\u3059\u3002
\nCPU\u3084\u30e1\u30e2\u30ea\u306b\u3064\u3044\u3066\u306f\u300ccat \/proc\/cpuinfo\u300d\u3084\u300ccat \/proc\/meminfo\u300d\u3067\u78ba\u8a8d\u3067\u304d\u307e\u3059\u304c\u3001\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306e\u901f\u5ea6\u306b\u3064\u3044\u3066\u306f\u5b9f\u6e2c\u3057\u3066\u307f\u306a\u3044\u3053\u3068\u306b\u306f\u308f\u304b\u308a\u307e\u305b\u3093\u3002<\/p>\n
\u7279\u306bAWS Lambda\u3092\u30ea\u30fc\u30b8\u30e7\u30f3\u9593\u3092\u307e\u305f\u3044\u3060\u30d0\u30c3\u30c1\u51e6\u7406\u306a\u3069\u306b\u4f7f\u7528\u3059\u308b\u969b\u306b\u306f\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u901f\u5ea6\u306f\u91cd\u8981\u306b\u306a\u3063\u3066\u304d\u307e\u3059\u3002
\n\u305d\u3053\u3067\u4eca\u56de\u306fAWS Lambda Python\u3067Lambda\u95a2\u6570\u5b9f\u884c\u30b5\u30fc\u30d0\u306e\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u30b9\u30d4\u30fc\u30c9\u30c6\u30b9\u30c8\u3092\u884c\u3046\u65b9\u6cd5\u3092\u5099\u5fd8\u9332\u3068\u3057\u3066\u8a18\u8f09\u3057\u3066\u304a\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n
\r\n[magtranetwork@localhost ~]$ mkdir -p speedtest\r\n[magtranetwork@localhost ~]$ cd speedtest\r\n[magtranetwork@localhost speedtest]$ curl -Lo speedtest-cli https:\/\/raw.githubusercontent.com\/sivel\/speedtest-cli\/master\/speedtest.py\r\n<\/pre>\n\r\n[magtranetwork@localhost speedtest]$ vim lambda_function.py\r\n# -*- coding:utf-8 -*-\r\n\r\nimport commands\r\nimport os\r\n\r\nprint('Loading function')\r\n\r\n#Linux\u306e\u30b7\u30a7\u30eb\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u95a2\u6570\r\ndef _(cmd):\r\n return commands.getoutput(cmd)\r\n\r\n#Lambda\u30e1\u30a4\u30f3\u95a2\u6570\r\ndef lambda_handler(event, context):\r\n\r\n #\u30c6\u30b9\u30c8\u30a4\u30d9\u30f3\u30c8JSON\u306bserver_list\u306e\u30ad\u30fc\u304c\u3042\u308a\u3001\u8981\u7d20\u304c\u7a7a\u3067\u306a\u3051\u308c\u3070list\u8868\u793a\r\n if event.has_key("server_list") == True and event["server_list"] != "":\r\n #server_list\u306e\u8981\u7d20\u3092\u691c\u7d22\u30ef\u30fc\u30c9\u3068\u3057\u3066list\u306e\u7d50\u679c\u3092\u691c\u7d22\r\n search_key = event["server_list"].strip()\r\n print(_(".\/speedtest-cli --list | grep " + search_key))\r\n return\r\n #\u30c6\u30b9\u30c8\u30a4\u30d9\u30f3\u30c8JSON\u306bserver_number\u306e\u30ad\u30fc\u304c\u306a\u3044\u3001\u307e\u305f\u306f\u8981\u7d20\u304c\u7a7a\u3067\u3042\u308c\u3070server\u3092\u6307\u5b9a\u305b\u305a\u306b\u5b9f\u884c\r\n if event.has_key("server_number") == False or event["server_number"] == "":\r\n print("Exec: .\/speedtest-cli")\r\n print(_(".\/speedtest-cli"))\r\n else:\r\n #\u30c6\u30b9\u30c8\u30a4\u30d9\u30f3\u30c8JSON\u306bserver_number\u306e\u30ad\u30fc\u304c\u3042\u308a\u3001\u8981\u7d20\u304c\u7a7a\u3067\u306a\u3051\u308c\u3070server\u304clist\u306b\u5b58\u5728\u3059\u308b\u304b\u3092\u78ba\u8a8d\r\n server_number = event["server_number"].strip()\r\n is_server=_(".\/speedtest-cli --list | grep " + server_number + "\\) | wc -l").strip()\r\n #server_number\u304clist\u306b\u5b58\u5728\u3059\u308c\u3070server\u3092\u6307\u5b9a\u3057\u3066\u5b9f\u884c\u3001\u5b58\u5728\u3057\u306a\u3051\u308c\u3070\u30a8\u30e9\u30fc\u3067\u7d42\u4e86\r\n if is_server == "1":\r\n print("Exec: .\/speedtest-cli --server " + server_number)\r\n print(_(".\/speedtest-cli --server " + server_number))\r\n else:\r\n print("Inputed server number is invalid.")\r\n<\/pre>\n\r\n[magtranetwork@localhost speedtest]$ chmod 777 -R *\r\n[magtranetwork@localhost speedtest]$ zip -r speedtest.zip *\r\n<\/pre>\nAWS Lambda Python\u95a2\u6570speedtest\u306e\u30c6\u30b9\u30c8\u30a4\u30d9\u30f3\u30c8\u306e\u6307\u5b9a\u65b9\u6cd5<\/h3>\n
\u4e0a\u8a18\u3067\u4f5c\u6210\u3057\u305fspeedtest.zip\u3092\u5b9f\u884c\u3059\u308b\u969b\u306b\u306f\u30a4\u30d9\u30f3\u30c8\u60c5\u5831\u3068\u3057\u3066\u4e0b\u8a18\u306e\u3088\u3046\u306b\u30ad\u30fc\u3068\u8981\u7d20\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u3001\u30b5\u30fc\u30d0\u30ea\u30b9\u30c8\u306e\u691c\u7d22\u3001\u30b5\u30fc\u30d0\u756a\u53f7\u3092\u6307\u5b9a\u3057\u3066\u306e\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u30b9\u30d4\u30fc\u30c9\u30c6\u30b9\u30c8\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n
\u30b5\u30fc\u30d0\u756a\u53f7\u306elist\u3092\u8868\u793a\u3059\u308b\u5834\u5408\u306fserver_list\u30ad\u30fc\u306b\u30b5\u30fc\u30d0\u30ea\u30b9\u30c8\u304b\u3089\u691c\u7d22\u3057\u305f\u3044\u6587\u5b57\u5217(Japan, Tokyo\u306a\u3069)\u3092\u8981\u7d20\u3068\u3057\u3066\u6307\u5b9a\u3057\u3066\u5b9f\u884c\u3059\u308b\u3002<\/p>\n
\r\n{\r\n "server_list": "Japan",\r\n "server_number": ""\r\n}\r\n<\/pre>\n\u30b5\u30fc\u30d0\u3092\u6307\u5b9a\u3057\u3066\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u30b9\u30d4\u30fc\u30c9\u6e2c\u5b9a\u3092\u3059\u308b\u5834\u5408\u306fserver_number\u30ad\u30fc\u306b\u4f7f\u7528\u3057\u305f\u3044\u30b5\u30fc\u30d0\u756a\u53f7\u3092\u8981\u7d20\u3068\u3057\u3066\u6307\u5b9a\u3057\u3066\u5b9f\u884c\u3059\u308b\u3002<\/p>\n
\r\n{\r\n "server_list": "",\r\n "server_number": "6424"\r\n}\r\n<\/pre>\n\u30b5\u30fc\u30d0\u3092\u81ea\u52d5\u9078\u629e\u3057\u3066\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u30b9\u30d4\u30fc\u30c9\u6e2c\u5b9a\u3092\u3059\u308b\u5834\u5408\u306f\u30ad\u30fc\u306b\u4f55\u3082\u5165\u529b\u305b\u305a\u5b9f\u884c\u3059\u308b\u3002<\/p>\n
\r\n{\r\n "server_list": "",\r\n "server_number": ""\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"AWS Lambda\u3092\u4f7f\u7528\u3059\u308b\u4e0a\u3067Lambda\u95a2\u6570\u3092\u5b9f\u884c\u3059\u308b\u30b5\u30fc\u30d0\u304c\u3069\u306e\u7a0b\u5ea6\u306e\u6027\u80fd\u3092\u6301\u3063\u3066\u3044\u308b\u304b\u306f\u6c17\u306b\u306a\u308b\u3068\u3053\u308d\u3067\u3059\u3002 CPU\u3084\u30e1\u30e2\u30ea\u306b\u3064\u3044\u3066\u306f\u300ccat \/proc\/cpuinfo\u300d\u3084\u300ccat \/proc\/meminfo […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[3,31,79,80],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/posts\/1892"}],"collection":[{"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/comments?post=1892"}],"version-history":[{"count":6,"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/posts\/1892\/revisions"}],"predecessor-version":[{"id":4905,"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/posts\/1892\/revisions\/4905"}],"wp:attachment":[{"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/media?parent=1892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/categories?post=1892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.magtranetwork.com\/wp-json\/wp\/v2\/tags?post=1892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}