{"id":381,"date":"2017-06-30T15:00:00","date_gmt":"2017-06-30T06:00:00","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2017\/06\/30\/integrate_datadog_lambda"},"modified":"2017-06-30T15:00:00","modified_gmt":"2017-06-30T06:00:00","slug":"integrate_datadog_lambda","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2017\/06\/30\/integrate_datadog_lambda\/","title":{"rendered":"Datadog \u00d7 API Gateway \u00d7 Lambda\u3067\u30d7\u30ed\u30bb\u30b9\u306e\u518d\u8d77\u52d5\u3092\u81ea\u52d5\u5316\u3059\u308b"},"content":{"rendered":"

MMM\u30b5\u30fc\u30d0\u30b5\u30a4\u30c9\u30a8\u30f3\u30b8\u30cb\u30a2\u306e\u67f3\u6cbc\u3067\u3059\u3002
\n\u304a\u4e16\u8a71\u306b\u306a\u3063\u3066\u304a\u308a\u307e\u3059\u3002
\n\u3088\u3046\u3084\u304f\u5317\u6d77\u9053\u3082\u6691\u304f\u306a\u3063\u3066\u304d\u307e\u3057\u305f\u3002<\/p>\n

\u524d\u56de<\/a>\u306b\u5f15\u304d\u7d9a\u3044\u3066Datadog\u3067\u66f8\u3044\u3066\u307f\u307e\u3059\u3002
\n\u30b7\u30f3\u30d7\u30eb\u306a\u30db\u30b9\u30c8\u76e3\u8996\u3060\u3051\u3067\u306a\u304f\u3001\u3061\u3087\u3063\u3068\u30c8\u30ea\u30c3\u30ad\u30fc\u306a\u3053\u3068\u3082\u3084\u3063\u3066\u307f\u3088\u3046\u3068\u3044\u3046\u3053\u3068\u3067\u3001Lambda\u3092Datadog\u3068\u7d44\u307f\u5408\u308f\u305b\u3066\u307f\u307e\u3059\u3002<\/p>\n

\u4eca\u56de\u306e\u30b4\u30fc\u30eb\u306f\u3001 EC2\u30ce\u30fc\u30c9\u4e0a\u3067\u8d70\u3063\u3066\u3044\u308b\u306f\u305a\u306enginx\u30d7\u30ed\u30bb\u30b9\u304c\u6b62\u307e\u3063\u3066\u3044\u305f\u5834\u5408\u3001\u81ea\u52d5\u3067\u518d\u8d77\u52d5\u3059\u308b<\/code> \u3053\u3068\u306b\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n

\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3<\/h3>\n

\u3053\u3093\u306a\u611f\u3058\u3067\u4f5c\u3063\u3066\u307f\u307e\u3059\u3002<\/p>\n

\"\"<\/p>\n

EC2\u306e\u6e96\u5099<\/h3>\n

EC2\u3092\u666e\u901a\u306b\u4f5c\u308a\u307e\u3059\u3002
\n\"\"<\/p>\n

PENDING<\/code> \u304b\u3089 RUNNING<\/code> \u306b\u5909\u308f\u3063\u305f\u3089\u3001\u30db\u30b9\u30c8\u306bssh\u3057\u3066\u3001nginx\u3068Datadog\u306e\u30a8\u30fc\u30b8\u30a7\u30f3\u30c8\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u3044\u304d\u307e\u3059\u3002
\n\u63a5\u7d9a\u5148\u306e\u30a2\u30c9\u30ec\u30b9\u306f\u74b0\u5883\u306b\u3088\u3063\u3066\u5909\u308f\u308a\u307e\u3059\u3002
\npem\u30d5\u30a1\u30a4\u30eb\u306f\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u3063\u305f\u6642\u306e\u3082\u306e\u3067\u3059\u3002<\/p>\n

chmod 600 ~\/.ssh\/datadog_lambda_test_yagi.pem<\/code><\/pre>\n
ssh ec2-user@xxx.xxx.xxx.xx -i ~\/.ssh\/datadog_lambda_test_yagi.pem<\/code><\/pre>\n

nginx\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/p>\n

sudo yum install nginx<\/code><\/pre>\n

\u305d\u3057\u3066\u3001Datadog\u306e\u30a8\u30fc\u30b8\u30a7\u30f3\u30c8\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002\u524d\u56de\u306e\u8a18\u4e8b<\/a> \u3092\u5fa1\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n

\u307e\u305f\u3001nginx\u306e\u30d7\u30ed\u30bb\u30b9\u76e3\u8996\u306f\u3001\u8a2d\u5b9a\u304c\u5fc5\u8981\u306a\u305f\u3081\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n

sudo cp \/etc\/dd-agent\/conf.d\/process.yaml.example \/etc\/dd-agent\/conf.d\/process.yaml<\/code><\/pre>\n
sudo vi \/etc\/dd-agent\/conf.d\/process.yaml<\/code><\/pre>\n

\u30d5\u30a1\u30a4\u30eb\u306e\u6700\u4e0b\u90e8\u306b<\/p>\n

  - name: nginx\n    search_string: ['nginx']<\/code><\/pre>\n

\u3068\u66f8\u304f\u3053\u3068\u3067\u3001nginx\u30d7\u30ed\u30bb\u30b9\u306e\u76e3\u8996\u304c\u3067\u304d\u307e\u3059\u3002
\n\u7de8\u96c6\u5f8c\u306f\u5fc5\u305a
\nsudo \/etc\/init.d\/datadog-agent restart<\/code> \u3067\u3001\u30a8\u30fc\u30b8\u30a7\u30f3\u30c8\u306e\u518d\u8d77\u52d5\u3092\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n

API Gateway\u3092\u4f5c\u6210<\/h3>\n

API Gateway\u306f\u7c21\u5358\u306bWeb API\u304c\u4f5c\u6210\u3067\u304d\u308bAWS\u306e\u30de\u30cd\u30fc\u30b8\u30c9\u30b5\u30fc\u30d3\u30b9\u3067\u3059\u3002<\/p>\n

AWS\u30b3\u30f3\u30bd\u30fc\u30eb\u306e\u3001 API Gateway<\/code> -> \u65b0\u3057\u3044API\u306e\u4f5c\u6210<\/code> \u304b\u3089\u4f5c\u3063\u3066\u3044\u304d\u307e\u3059\u3002
\nAPI\u540d\u306f\u3001\u4efb\u610f\u306e\u540d\u524d\u3067\u5927\u4e08\u592b\u3067\u3059\u304c\u3001\u4eca\u56de\u306f datadog_lambda_test_api<\/code> \u3068\u3057\u307e\u3057\u305f\u3002<\/p>\n

\"\"<\/p>\n

\u6b21\u306b\u30ea\u30bd\u30fc\u30b9\u3092\u4f5c\u3063\u3066\u3044\u304d\u307e\u3059\u3002 \u30a2\u30af\u30b7\u30e7\u30f3<\/code> -> \u30ea\u30bd\u30fc\u30b9\u306e\u4f5c\u6210<\/code> \u3067\u3001API\u3078\u306e\u30ea\u30bd\u30fc\u30b9(\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8)\u3092\u4f5c\u308a\u307e\u3059\u3002
\n\"\"<\/p>\n

\u6b21\u306b\u30e1\u30bd\u30c3\u30c9\u3092\u4f5c\u308a\u307e\u3059\u3002\u30e1\u30bd\u30c3\u30c9\u306fHTTP\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002
\n\u304d\u3061\u3093\u3068\u3057\u305fAPI\u8a2d\u8a08\u306f\u4eca\u56de\u306f\u305b\u305a\u3001\u5358\u306bPOST\u3067\u4f5c\u3063\u3066\u307f\u307e\u3059\u3002
\n\u5148\u7a0b\u4f5c\u3063\u305f\u30ea\u30bd\u30fc\u30b9\u3092\u9078\u629e\u3057\u305f\u72b6\u614b\u3067\u3001 \u30a2\u30af\u30b7\u30e7\u30f3<\/code> -> \u30e1\u30bd\u30c3\u30c9\u306e\u4f5c\u6210<\/code> \u3067\u4f5c\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
\n\"\"<\/p>\n

\u7d71\u5408\u30bf\u30a4\u30d7<\/code> \u306b\u306f Lambda\u95a2\u6570<\/code> \u3092\u9078\u629e\u3057\u307e\u3059\u3002 Lambda\u95a2\u6570<\/code> \u306b\u95a2\u6570\u540d\u3092\u5165\u529b\u3057\u305f\u3044\u3068\u3053\u308d\u3067\u3059\u304c\u3001\u307e\u3060\u95a2\u6570\u3092\u4f5c\u3063\u3066\u3044\u306a\u3044\u306e\u3067\u3001\u4f5c\u308a\u307e\u3059\u3002<\/p>\n

Lambda\u30d5\u30a1\u30f3\u30af\u30b7\u30e7\u30f3\u3092\u4f5c\u6210<\/h3>\n

BluePrint\u306b\u306f Blank Function<\/code> \u3092\u9078\u3093\u3067\u304a\u304d\u307e\u3059\u3002
\n\"\"<\/p>\n

\u30c8\u30ea\u30ac\u30fc\u306f API Gateway<\/code> \u3092\u9078\u629e\u3057\u3066\u3001\u5148\u7a0b\u4f5c\u6210\u3057\u305fAPI\u3092\u9078\u3093\u3067\u304a\u3044\u3066\u304f\u3060\u3055\u3044\u3002
\n\"\"<\/p>\n

\u4eca\u56de\u306fPython 2.7\u3067\u66f8\u3044\u3066\u3044\u304d\u307e\u3059\u3002\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f\u3053\u3093\u306a\u611f\u3058\u3067\u3059\u3002<\/p>\n

import boto3\nimport paramiko\n\ndef lambda_handler(event, context):\n    s3_client = boto3.client('s3')\n    # Download pem from S3 bucket\n    s3_client.download_file('lambda.datadog.test',\n            'datadog_lambda_test_yagi.pem', '\/tmp\/datadog_lambda_test_yagi.pem')\n\n    key = paramiko.RSAKey.from_private_key_file("\/tmp\/datadog_lambda_test_yagi.pem")\n    client = paramiko.SSHClient()\n    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n\n    host=event['your IP']\n    print "Connecting to " + host\n    client.connect( hostname = host, username = "ec2-user", pkey = key )\n    print "Connected to " + host\n\n    commands = [\n        "sudo service nginx stop", \n        "sudo service nginx start"\n        ]\n    for command in commands:\n        print "Executing {}".format(command)\n        stdin , stdout, stderr = c.exec_command(command)\n        print stdout.read()\n        print stderr.read()\n\n    return\n    {\n        'message' : "script completed."\n    }<\/code><\/pre>\n

import\u3057\u3066\u3044\u308bboto3\u306f\u3001aws cli\u3092\u4f7f\u3046\u305f\u3081\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002
\nSSH\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b
paramiko<\/a>\u3092\u4f7f\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n

commands<\/code> \u306e\u90e8\u5206\u306b\u3001\u597d\u304d\u306a\u30b3\u30de\u30f3\u30c9\u3092\u66f8\u304f\u3053\u3068\u3067\u3001SSH\u3057\u305f\u5f8c\u306b\u305d\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n

\u307e\u305f\u3001\u3054\u89a7\u306e\u901a\u308a\u3001pem\u30d5\u30a1\u30a4\u30eb\u3092S3\u304b\u3089\u53d6\u5f97\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u306f\u3053\u306e\u5f8c\u3067\u3084\u3063\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n

Lambda\u30d5\u30a1\u30f3\u30af\u30b7\u30e7\u30f3\u3092Python\u3067\u66f8\u304f\u6642\u3001 paramiko<\/code> \u306e\u3088\u3046\u306a\u5916\u90e8\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3046\u3068\u304d\u306f\u3001\u30a4\u30f3\u30e9\u30a4\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u304c\u3067\u304d\u307e\u305b\u3093\u3002
\npip\u3067\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u3001zip\u306b\u56fa\u3081\u3066\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002
\u30c7\u30d7\u30ed\u30a4\u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u4f5c\u6210 (Python)<\/a>\u3092\u53c2\u8003\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n

\u3053\u3053\u307e\u3067\u3067\u304d\u308c\u3070\u3001Lambda\u30d5\u30a1\u30f3\u30af\u30b7\u30e7\u30f3\u304c\u5b8c\u6210\u3057\u307e\u3059\u3002\u30d5\u30a1\u30f3\u30af\u30b7\u30e7\u30f3\u540d\u3092API Gateway\u5074\u306b\u8a2d\u5b9a\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n

S3\u306bpem\u3092\u7f6e\u3044\u3066\u304a\u304f<\/h3>\n

\u8aac\u660e\u304c\u524d\u5f8c\u3057\u307e\u3057\u305f\u304c\u3001\u4e0a\u8a18\u306ePython\u30bd\u30fc\u30b9\u306e\u3088\u3046\u306b\u3001EC2\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u305f\u3081\u306epem\u30d5\u30a1\u30a4\u30eb\u3092\u7f6e\u3044\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n

\"\"<\/p>\n

\u30d0\u30b1\u30c3\u30c8\u306e\u4f5c\u6210\u30fbpem\u306e\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3092\u3057\u3066\u304a\u304d\u307e\u3059\u3002\u30d0\u30b1\u30c3\u30c8\u540d\u30fb\u30d1\u30b9\u304c\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3068\u4e00\u81f4\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u304a\u304d\u307e\u3059\u3002
\n\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u5f8c\u3067\u3059\u3002<\/p>\n

\"\"<\/p>\n

Datadog\u306e\u30a2\u30e9\u30fc\u30c8\u8a2d\u5b9a<\/h3>\n

\u6700\u5f8c\u306b\u3001Datadog\u304b\u3089nginx\u30d7\u30ed\u30bb\u30b9\u3092\u76e3\u8996\u3057\u3066\u3001\u30d7\u30ed\u30bb\u30b9\u304c\u843d\u3061\u3066\u3044\u305f\u3089API\u3092\u53e9\u304f\u3001\u3068\u3044\u3046\u8a2d\u5b9a\u3092\u3057\u307e\u3059\u3002<\/p>\n

Datadog\u30b3\u30f3\u30bd\u30fc\u30eb\u304b\u3089 Integrations<\/code> -> Webhook<\/code> \u3092\u9078\u629e\u3057\u307e\u3059\u3002<\/p>\n

\"\"<\/p>\n

\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8\u306fAPI Gateway\u3092\u30c7\u30d7\u30ed\u30a4\u3057\u305f\u5f8c\u306b\u8868\u793a\u3055\u308c\u308b\u306e\u3067\u3001\u30ea\u30bd\u30fc\u30b9\u3092\u542b\u3081\u3066\u66f8\u304d\u307e\u3059\u3002<\/p>\n

\u30a2\u30e9\u30fc\u30c8\u306f Monitors<\/code> -> process<\/code> \u304b\u3089\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/p>\n

\"\"<\/p>\n

pick a process<\/code> \u306b process:nginx<\/code> \u3092\u9078\u629e\u3001\u30a2\u30e9\u30fc\u30c8\u901a\u77e5\u5148\u306b\u5148\u7a0b\u4f5c\u6210\u3057\u305fWebhook\u3092\u9078\u629e\u3057\u307e\u3059\u3002<\/p>\n

\u3053\u308c\u3067\u6700\u521d\u306e\u56f3\u306e\u901a\u308a\u3001\u6e96\u5099\u304c\u5168\u3066\u5b8c\u4e86\u3057\u307e\u3057\u305f\u3002
\n\u3082\u3046\u4e00\u5ea6\u898b\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n

\"\"<\/p>\n

\u52d5\u4f5c\u78ba\u8a8d<\/h3>\n

\u52d5\u4f5c\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3059\u3002
\nEC2\u30ce\u30fc\u30c9\u306b\u5165\u3063\u3066nginx\u3092\u6b62\u3081\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n

$ ps aux | grep nginx\nroot     24345  0.0  0.1  58104  1020 ?        Ss   06:00   0:00 nginx: master process \/usr\/sbin\/nginx -c \/etc\/nginx\/nginx.conf\nnginx    24347  0.0  0.3  58476  3952 ?        S    06:00   0:00 nginx: worker process\nec2-user 24349  0.0  0.2 110456  2192 pts\/0    S+   06:00   0:00 grep --color=auto nginx<\/code><\/pre>\n

nginx\u30d7\u30ed\u30bb\u30b9\u3092kill\u3057\u3066\u5c11\u3057\u5f85\u3061\u307e\u3059\u3002
\nCloudWatch\u306e\u30ed\u30b0\u306b<\/p>\n

{\n    'message' : "script completed."\n}<\/code><\/pre>\n

\u3068\u51fa\u3066\u3001nginx\u3082\u7121\u4e8b\u306b\u518d\u8d77\u52d5\u3057\u3066\u3044\u308c\u3070\u3001\u7121\u4e8b\u6210\u529f\u3067\u3059\u3002<\/p>\n

\u307e\u3068\u3081<\/h3>\n

\u4eca\u56de\u306f\u30d7\u30ed\u30bb\u30b9\u518d\u8d77\u52d5\u3067\u3057\u305f\u304c\u3001\u30db\u30b9\u30c8\u306bSSH\u63a5\u7d9a\u3057\u3066\u30b3\u30de\u30f3\u30c9\u304c\u6253\u3066\u308b\u3068\u3044\u3046\u3053\u3068\u306f\u3001\u4e8b\u5b9f\u4e0a\u306a\u3093\u3067\u3082\u3067\u304d\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002
\n\u4eca\u5f8c\u3082Datadog\u3092\u4f7f\u3063\u305f\u30bd\u30ea\u30e5\u30fc\u30b7\u30e7\u30f3\u3092\u30ac\u30f3\u30ac\u30f3\u8a66\u3057\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059!<\/p>\n

\u307e\u305f\u3001AWS Lambda\u3084Datadog\u3092\u7528\u3044\u305f\u76e3\u8996\u306b\u3064\u3044\u3066\u306f\u3001\u4ee5\u4e0b\u306e\u307a\u30fc\u30b8\u3067\u3082\u7d39\u4ecb\u3057\u3066\u3044\u307e\u3059\u3002\u305c\u3072\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n

\u30fb\u30b5\u30fc\u30d0\u30fc\u30ec\u30b9\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\uff08AWS Lambda\uff09<\/a><\/p>\n

\u30fb\u30af\u30e9\u30a6\u30c9\u904b\u7528\u76e3\u8996(Datadog)<\/a><\/p>\n

\u53c2\u8003<\/h3>\n