github.com/google/cloudprober@v0.11.3/servers/external/external.go (about) 1 // Copyright 2020 The Cloudprober Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package external adds support for an external server. 16 package external 17 18 import ( 19 "context" 20 "fmt" 21 "os/exec" 22 23 "github.com/google/cloudprober/logger" 24 "github.com/google/cloudprober/metrics" 25 configpb "github.com/google/cloudprober/servers/external/proto" 26 "github.com/google/shlex" 27 ) 28 29 // Server implements a external command runner. 30 type Server struct { 31 c *configpb.ServerConf 32 l *logger.Logger 33 34 cmdName string 35 cmdArgs []string 36 cmd *exec.Cmd 37 } 38 39 // TODO 40 // 1. Export health status (pid-file OR by monitoring the process started.) 41 // 2. Add support for command-line substitution. 42 // 2. Add auto-restart support. 43 44 // New creates a new external server. 45 func New(initCtx context.Context, c *configpb.ServerConf, l *logger.Logger) (*Server, error) { 46 cmdParts, err := shlex.Split(c.GetCommand()) 47 if err != nil { 48 return nil, fmt.Errorf("error parsing command line (%s): %v", c.GetCommand(), err) 49 } 50 51 return &Server{ 52 c: c, 53 l: l, 54 cmdName: cmdParts[0], 55 cmdArgs: cmdParts[1:len(cmdParts)], 56 }, nil 57 } 58 59 // Start runs the external command. 60 func (s *Server) Start(ctx context.Context, dataChan chan<- *metrics.EventMetrics) error { 61 s.cmd = exec.CommandContext(ctx, s.cmdName, s.cmdArgs...) 62 go func() { 63 err := s.cmd.Run() 64 s.l.Infof("Command %s started. Err status: %v", s.c.GetCommand(), err) 65 }() 66 67 return nil 68 }