github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/util/sysctl/sysctl.go (about)

     1  /*
     2  Copyright 2015 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package sysctl
    18  
    19  import (
    20  	"io/ioutil"
    21  	"path"
    22  	"strconv"
    23  	"strings"
    24  )
    25  
    26  const (
    27  	sysctlBase         = "/proc/sys"
    28  	VmOvercommitMemory = "vm/overcommit_memory"
    29  	VmPanicOnOOM       = "vm/panic_on_oom"
    30  	KernelPanic        = "kernel/panic"
    31  	KernelPanicOnOops  = "kernel/panic_on_oops"
    32  
    33  	VmOvercommitMemoryAlways    = 1 // kernel performs no memory over-commit handling
    34  	VmPanicOnOOMInvokeOOMKiller = 0 // kernel calls the oom_killer function when OOM occurs
    35  
    36  	KernelPanicOnOopsAlways  = 1  // kernel panics on kernel oops
    37  	KernelPanicRebootTimeout = 10 // seconds after a panic for the kernel to reboot
    38  )
    39  
    40  // GetSysctl returns the value for the specified sysctl setting
    41  func GetSysctl(sysctl string) (int, error) {
    42  	data, err := ioutil.ReadFile(path.Join(sysctlBase, sysctl))
    43  	if err != nil {
    44  		return -1, err
    45  	}
    46  	val, err := strconv.Atoi(strings.Trim(string(data), " \n"))
    47  	if err != nil {
    48  		return -1, err
    49  	}
    50  	return val, nil
    51  }
    52  
    53  // SetSysctl modifies the specified sysctl flag to the new value
    54  func SetSysctl(sysctl string, newVal int) error {
    55  	return ioutil.WriteFile(path.Join(sysctlBase, sysctl), []byte(strconv.Itoa(newVal)), 0640)
    56  }